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 tab

One can use \o{...} to denote a code point written in octal:

"\o{101}"   # == "A"
"\o{123}"   # == "S"
"\o{12}"    # == "\n"
"\o{1}"     # string with one character with code point 1

Or \x{...} and specify hexadecimal numbers:

"\x{41}"    # == "A"
"\x{263a}"  # == "☺"

To specity Unicode names, one can use \N{...}:

"\N{WHITE SMILING FACE}"            # == "☺"
"\N{GREEK CAPITAL LETTER GAMMA}"    # == "Γ"

A string can span multiple lines:

"hello
world"    # same as "hello\nworld"

For writing a string that has many double quotes, parenthesis, or similar characters, one can use alternative literals:

# Supports double quotes and nested parenthesis
%(hello ("world"))  # same as "hello (\"world\")"

# Supports double quotes and nested brackets
%[hello ["world"]]  # same as "hello [\"world\"]"

# Supports double quotes and nested curlies
%{hello {"world"}}  # same as "hello {\"world\"}"

# Supports double quotes and nested angles
%<hello <"world">>  # same as "hello <\"world\">"

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:

"sum = #{1 + 2}"        # "sum = 3"

Single quoted strings

Single quoted strings does not support escapes, nor interpolation.

'single\tquoted'   # creates a string as it is

For specifying a custom delimiter, one can use %q followed by any non-whitespace delimiter:

%q(hello ('world'))     # same as 'hello (\'world\')'

Another way of writing string literals, is by placing a colon in font of an alphanumeric string that begins with a letter.

:word          # == 'word'
:another_word  # == 'another_word'

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:

<<"EOF";
a = #{1+2}
b = #{3+4}
EOF

If single quotes are used, then the here document will not support interpolation, like a normal single-quoted string:

<<'FOO';
No
#{interpolation}
here
FOO

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:

(<<EOF + "lamb");
Mary had
  a little
EOF

which is equivalent with:

(<<EOF
Mary had
  a little
EOF
+ "lamb");