$ (String)

Rax string type is denoted by the symbol $. String literals are written in double quotes: "ala ma kota". The maximum length of a Rax string is 3200 characters. Unlike in C, strings are a separate type, not an array. Rax features many string operators. Operators defined on strings are listed in the table below:

Table 3.4. Operators defined on $

s1 := s2Assignment
s1 == s2Equals
s1 != s2Not equals
s1 > s2Greater than (alphabetical order)
s1 < s2Less than (alphabetical order)
s1 >= s2Greater than or equal to (alphabetical order)
s1 <= s2Less than of equal to (alphabetical order)
#sReturns the length of string s, for example: #"ala ma kota" == 11.
-sReturns the reverse of string s, for example: -"ala ma kota" == "atok am ala"
s1 + s2Concatenate string s1 and s2, for example: "ala" + "ma" + "kota" == "alamakota"
s - pRemove pattern p from string s. Also allow % as a wild card. Use \% to escape %. For example:
"aap" - "a" == "ap" "aap noot poes" - "%p " == "noot poes" "aap noot poes" - "n%t " == "aap poes"
s * nRepeat string s n number of times, for example: "ala" * 3 == "alaalaala"
set_of_strings * sepConcatenate strings in set_of_strings using sep as a separator. For example: {"ala", "ma", "kota"} * " " == "ala ma kota"
s / dSplit string s using string d as a delimiter, for example: "ala ma kota" / " " == {"ala", "ma", "kota"}
s =~ r Match string s to the regex r, for example: `print "aap" =~ "a+[o-q]" would evaluate to true. See Chapter 9, Regex (Regular Expression).
s !~ r Unmatch string s to the regex r, for example: `print "noot" !~ "a+[o-q]" would evaluate to true, i.e., "noot" does not match. See Chapter 9, Regex (Regular Expression).

Rax supports an escape sequence \x for inserting an ASCII character by hex value. To insert the ASCII character with hex value øø use: \xøø. An escape sequence can be used to insert a non-printable (or printable) ASCII character into a string. Traditionally, languages that use a C-like syntax feature escape shorthands for frequently used escape sequences. For convenience, Rax supports C-like escape shorthands for line feed (or new line) (\n), carriage return (\r), horizontal tab (\t), backslash (\\), and double quote (\"). For example: "\tala ma \"kota\"\n".

Below a table with the more commonly used escape sequences for double quoted strings.

Table 3.5. Escape Sequences for Double Quoted Literal Strings

Any hex ASCII value øø.\xøø
Horizontal Tab\x09\t
Line Feed\x0A\n
Vertical Tab\x0B
Form Feed\x0C
Carriage Return\x0D\r
Double Quote\x22\"

In some special cases it might be inconvenient to have to escape backslashes, especially when no (other) escaped characters are used. For example when specifying a path for MS-Windows (though forward slashes can be used in Rax) or when using regular expressions. This inconvenience is called the leaning toothpick syndrome. For this reason Rax also supports the single quoted strings, often referred to as raw strings. Raw strings are written in single quotes: 'ala ma kota'. A path expressed like this: "C:\\Program Files\\RaxDB\\rax.exe" can also be expressed as: 'C:\Program Files\RaxDB\rax.exe' or as: "C:/Program Files/RaxDB/rax.exe". Similarly the regular expression: "^(\\n)*\\$" can also be expressed as: '^(\n)*\$'.

In a raw string, every character stands for itself with one obvious exception: the single quote. To insert a single quote in a raw string use two single quotes in a row. Therefore 'I''m all ears.' is equal to "I'm all ears." Rax, like C, concatenates all consecutive literal strings. For example this expression "aap noot" is equivalent to "aap " "noot". It is even allowed, again like in C, to have comments in between two literal strings, as shown in this code snipped:

    $: rx := '[a-z]*'             // Zero to many lowercase.
             '[A-Z]+';            // Followed by at least one upper.
    `print rx == '[a-z]*[A-Z]+';  // Output: true

Rax concatenates any mix of raw and non-raw strings. Additionally, Rax concatenates literal numbers to literal strings for convenience, which comes in handy when using predefined literal values, for example: $: here = "At line " __LINE__;. Since double single quotes in a raw string have this special meaning, whitespace can be a sine qua non for concatenation, as demonstrated in the code below:

    `print "aap""noot";   // Output: aapnoot
    `print "aap"'noot';   // Output: aapnoot
    `print 'aap''noot';   // Output: aap'noot
    `print 'aap' 'noot';  // Output: aapnoot

For the tech savvy reader it might be interesting to understand that Rax features yet an other type of literal string, namely the <, > delimited string for system includes. In the rare case a special character is needed, backslash escaping can be used like in double quoted strings with > taking the place of ". A valid but highly suspect include statement might look like this: %include <aap"\>noot>;. Test this at your own peril!

Rax string guidelines.  Since there are so many options, here are some guidelines for using strings in Rax: Coders are strongly advised to always use the (forward) slash as a path separator. Coders are strongly advised to use whitespace when concatenating literal strings. Coders are advised to use raw literal strings for use as regular expressions and double quoted literal strings for other string literal expressions.