A string is a sequence of characters. Strings can be manipulated in various ways to produce printed output. One enters a string by surrounding a sequence of characters with quotation marks.
i1 : "abcdefghij" |
Strings may contain newline characters.
i2 : "abcde |
Strings, like anything else, can be assigned to variables.
i3 : x = "abcdefghij" |
There are escape sequences that make it possible to enter special characters:
\n newline \f form feed \r return \\ \ \" " \t tab \xxx ascii character with octal value xxx
i4 : y = "abc\101\102\n\tstu" |
We can use peek to see what characters are in the string.
i5 : peek y |
Another way to enter special characters into strings is to use /// as the string delimiter.
i6 : ///a \ n = "c"/// |
The function ascii converts strings to lists of ascii character code, and back again.
i7 : ascii y |
i8 : ascii oo |
We may use the operator | to concatenate strings.
i9 : x|x|x |
The operator # computes the length of a string.
i10 : #x |
We may also use the operator # to extract single characters from a string. Warning: we number the characters starting with 0.
i11 : x#5 |
The function substring will extract portions of a string for us.
i12 : substring(x,5) |
i13 : substring(x,5,2) |
The class of all strings is String.