Strings
Strings
String literals use double quotes. Interpolated strings use backticks.
let plain = "a literal"
let name = "peko"
let greeting = `hello, ${name}`Any expression can appear inside ${ ... }, not just a name:
let message = `deployed ${project.name} ${project.version}`
let size = `${items.size()} items`
let path = `${root}/${segment}`Interpolation is the normal way to build strings. There is no format function in the language itself, and no concatenation operator is needed for the common case.
Escapes
Both literal forms accept the standard escapes:
| Escape | Meaning |
|---|---|
\n, \t, \r |
Newline, tab, carriage return. |
\\ |
A backslash. |
\" and \' |
A quote. |
\` |
A backtick, for use inside an interpolated string. |
\xNN |
A byte, written as two hex digits. |
\u{HEX} |
A unicode scalar, written as one to six hex digits. |
A malformed escape is a diagnostic rather than a silent pass, so a bad \u{...}
is reported where it appears.
Characters
char is a separate type from string. A character literal uses single quotes
and accepts the same escapes:
let tab = '\t'Working with strings
string is a class, so text operations are methods:
let text = "src/main.peko"
let size = text.size()
let cut = text.index_of("/")
let tail = text.substring(cut + 1, text.size())
let is_src = text.starts_with("src")Two behaviors worth knowing:
index_ofreturns-1when the needle is absent, so check before using the result as an offset. An empty needle matches at0.substringtakes a start and an end, not a start and a length.
Because string is a class, == on two strings routes to the Equals trait and
compares contents rather than identity.