Strings
A string is a sequence of text characters, surrounded by single quotes.
'Hello World!' //= ✓ OK "Hello World!" //= ✕ ERROR - Double-quotes (") are not valid. Hello World! //= ✕ ERROR - Missing quotes
Combining Strings ~
Strings can be joined together using the stringy operator ~
(tilde).
$firstName = 'Tal' $lastName = 'Turquoise' $fullName = $firstName ~ ' ' ~ $lastName //= 'Tal Turquoise' $numPaintings = 33 print($fullName ~ ' has ' ~ $numPaintings ~ ' paintings.') //= 'Tal Turquoise has 33 paintings.'
You can also use the combined stringy assignment operator ~=
, as a shortcut.
$postTitle = 'My Famous Taco Recipe' $postTitle ~= ' (33 comments)' //= 'My Famous Taco Recipe (33 comments)'
You can also use the fill
method to fill in {}
placeholders.
$message = '{} has {} tomatoes.' $message.fill('Taylor', 3) //= 'Taylor has 3 tomatoes.'
Multi-line Strings '''
Multi-line strings are surrounded with quote fences '''
.
Each quote fence must appear on a separate line from the text.
Surrounding whitespace and extra indentation will be automatically trimmed, so you can keep the formatting consistent with the surrounding code.
$poem = ''' Roses are red violets are blue THT has multi-line strings and other features also. '''
Special Characters
For convenience, backticks `
within a string are converted to single-quotes.
'That`s terrific!' //= 'That's terrific!'
Special characters can be used in strings, using backslash \
.
\n
- Newline (return)\t
- Tab
'This is line 1.\nThis is line 2.' //= This is line 1. //= This is line 2. 'Toledo\t33' //= 'Toledo 33' 'Tampa\t86' //= 'Tampa 86'
Escapes
Use a backslash \
to make an escape character appear as-is.
\`
- Backtick\\
- Backslash
'Backslash (\\) and Backtick (\`)' //= 'Backslash (\) and Backtick (`)'
Tip: You can use a multi-line string to avoid the extra backslashes.
$cleanerString = ''' I like `backticks` and newlines (\n) and tabs (\t) '''