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 Related Functions @count, @dump, @dumpvar, @explode, @find, @is_prefix, @is_string, @is_subsequence, @is_suffix, @member, @occurs, @push_back, @string2fun, @string2proc, @system, @Tracing, @UnTracing
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"