String
A String represents an immutable sequence of UTF-8 characters.
Double quoted strings
A String object is typically created with a string literal, enclosing UTF-8 characters in double quotes:
"hello world"Alternatively, we can create strings using the String class:
String("hello world")Being a new programming language, Sidef has also built-in support for Unicode quotation marks:
„double quoted” # == "double quoted"A backslash can be used to denote some characters inside the string:
"\"" # double quote
"\\" # backslash
"\e" # escape
"\f" # form feed
"\n" # newline
"\r" # carriage return
"\t" # tab
"\s" # space
"\v" # vertical tabOne can use \o{...} to denote a code point written in octal:
Or \x{...} and specify hexadecimal numbers:
To specity Unicode names, one can use \N{...}:
A string can span multiple lines:
For writing a string that has many double quotes, parenthesis, or similar characters, one can use alternative literals:
The Parser is aware of Unicode delimiters as well. Here are only a few examples:
Interpolation
Creating a String with embedded expressions, is called string interpolation:
Single quoted strings
Single quoted strings does not support escapes, nor interpolation.
For specifying a custom delimiter, one can use %q followed by any non-whitespace delimiter:
Another way of writing string literals, is by placing a colon in font of an alphanumeric string that begins with a letter.
Here-document
There must not be a space between the << and the token string. When the token string is double-quoted ("") or not quoted, the content will be interpolated like a double-quoted string:
If single quotes are used, then the here document will not support interpolation, like a normal single-quoted string:
The here document does not start immediately at the <<END token -- it starts on the next line. The <<END is actually an expression, whose value will be substituted by the contents of the here document. To further illustrate this fact, we can use the <<END inside a complex, nested expression:
which is equivalent with:
Last updated
Was this helpful?