String Value¶
String constants are written between quotes. To include a quote in a string, the quote must be escaped:
print "this is a string with a \" inside"
Others characters must be escaped in string: \n
is for end of line (or
carriage-return), \t
for tabulation, and \\
for
backslash.
Characters in a string can be accessed as if it was a tab (see sect. tab). Characters in a string are numbered starting from 0, so:
"abc"[1] ➙ "b"
Note that the result is a string with only one character. There is no specific type dedicated to the representation of just one character. The function @size can be used to get the number of characters in a string.
Notice also that strings are immutable values: contrary to tabs, it is not possible to change a character within a string. A new string must be constructed instead.
The operator +
corresponds to string concatenation:
$a := "abc" + "def"
print $a
will output on the console abcdef
. By extension, adding
any kind of value v to a string concatenates the string representation
of v to the string:
$a := 33
print ("abc" + $a)
will output "abc33"
. The value of the variable
$a
was automatically converted to type String before
being concatenated.
There are several String Management @car @cdr @char_is_alnum @char_is_alpha @char_is_ascii @char_is_blank @char_is_cntrl @char_is_digit @char_is_graph @char_is_lower @char_is_print @char_is_punct @char_is_space @char_is_upper @char_is_xdigit @copy @count @drop @dump @dumpvar @empty @explode @find @is_prefix @is_string @is_subsequence @is_suffix @last @member @occurs @parse @permute @push_back @r_compile @r_findall @r_match @r_search @remove @remove_duplicate @replace @scramble @slice @sort @split @sputter @string2fun @string2proc @strip_path @stutter @system @take @to_num @Tracing @UnTracing
Most functions acting on tabs operate also on strings.
The function @explode can be used to convert a string into a tab (vector) of characters (represented as string of size one). This makes it possible to use the functions defined for tabs. If a tab contains multiple strings, they can be concatenated using the @reduce operator to build a new string:
$t := @explode("123abc")
@assert $t == ["1", "2", "3", "a", "b", "c"]
$tt := @reduce (@+, $t)
@assert $tt == "123abc"