Strings
Strings in double quotes, "hello, world", understand escapes like \n, \t, \\ and \". Backticks avoid escaping:
> println("hello,\n world")
hello,
world
> println(`hello,\n world`)
hello,\n world
Extended delimiters
You can add backslashes before and after the outer quotes to avoid escaping inner ones:
> println(\"hello, "world""\)
hello, "world"
> println(\`hello, `world``\)
hello, `world`
In double quotes with backslashes, escapes need the number of backslashes to match inside and out:
> println(\\"hello,\n world"\\)
hello,\n world
> println(\\"hello,\\n world"\\)
hello,
world
Triple quotes
Triple-quote strings – with both """ and triple backticks – support escaping and extended delimiters in the same way. They also strip the indentation from the beginning of each line:
{
a = """
hello, world
"""
b = "hello, world"
test a == b
}
Template strings
A prefix on a string alters how it's interpreted, like a macro. Single-quote (backtick) templates put the tag before the string; triple-quote templates put it just after the opening quotes:
r`\d`
js`return 2+2`
```js
console.log("hello, world")
```
r gives you regular expressions and js inline JavaScript (see Calling JavaScript).
Regular expressions
Test a string with contains? and iterate over matches (and capture groups) with matches:
> contains?("1, 2, 3", r`\d`)
true
> collect(matches("1, 2, 3", r`\d`))
[["1"], ["2"], ["3"]]
Characters and encodings
Strings are sequences of unicode scalar values, represented as 21-bit integers in the Char type:
> collect("hello 🔥")
[c"h", c"e", c"l", c"l", c"o", c" ", c"🔥"]
> "hello 🔥"[7] == c"🔥"
true
> UInt32("hello 🔥"[7])
0x0001f525
Strings are abstracted from their storage format, and indexing is linear-time. You can get data views with constant-time access to code points, in a given encoding, with chars, utf16 and utf8:
> map(chars("🔥"), UInt32)
[0x0001f525]
> collect(utf16("🔥"))
[0xd83d, 0xdd25]
> collect(utf8("🔥"))
[0xf0, 0x9f, 0x94, 0xa5]
Note that graphemes may be composed from multiple Chars:
> collect("🤦🏼♂️")
[c"🤦", c"🏼", c"", c"♂", c"️"]
Building strings
string(x) converts a value to a string, and concat joins strings together:
concat("hello, ", "world")
string(42) # "42"
There's no string interpolation syntax yet; use concat and string.