The language / Strings
Start here01
The language02
- 01The basics
- 02Values and numbers
- 03Variables
- 04Calls and expressions
- 05Conditions and loops
- 06Strings
- 07Arrays and hashes
- 08Classes and records
- 09Traits and enums
- 10Blocks
- 11Methods
- 12Imports and exports
- 13Foreign files
- 14Failures
Programs and the world03
- 01Files, input and output
- 02JSON, time, URLs and random numbers
- 03Html and forms
- 04Templates: Ochre
- 05Built-in methods
Kiln04
02 The language / 06
Strings
Text: double quotes, interpolation, text blocks, and the methods that work on it.
Much of what a program does ends up as text for someone to read: a greeting, a report, a line in a log. In Hematite every string is written in double quotes, and values go into it by interpolation, with #{...}, so text and the values in it read in the order they appear. Text that runs over several lines has a form of its own, the text block.
Writing a string
A string goes between double quotes. Inside it, #{...} puts in the value of any expression:
const name = "Ada"
const age = 36
print("Hello, #{name}!") # prints Hello, Ada!
print("next year: #{age + 1}") # prints next year: 37- Strings use double quotes. Single quotes are an error.
- A string must end on the line where it starts. For a line break inside the text, write
\n, or use a text block. - Any Unicode is fine inside a string:
"blåbær 🦀". (Names are limited to plain letters, digits and_; the text in strings isn't.) #inside a string is plain text, not a comment.
Escapes
A backslash, \, starts an escape: a short way to write a character that would otherwise end the string, start an interpolation, or be hard to type.
| Write | To get |
|---|---|
\n | line break |
\t | tab |
\\ | \ |
\" | " |
\# | # (so "\#{x}" is the text #{x}) |
\u{e9} | the Unicode character U+00E9 (é); 1 to 6 hex digits |
Any other \ sequence is an error, so a stray backslash never slips through unnoticed.
Interpolation: #{...}
#{expression} inside a string inserts the expression's value as text. The expression can be anything that gives a value: a variable, arithmetic, a comparison, even a one-line if ... then ... else:
const name = "Ada"
const age = 36
const ok = true
print("#{name} is #{age} years old") # prints Ada is 36 years old
print("half: #{age / 2}") # prints half: 18.0
print("adult: #{age >= 18}") # prints adult: true
print("label: #{if ok then "yes" else "no"}") # prints label: yesValues appear exactly as print prints them, which is why age / 2 shows as 18.0: / always gives a Float.
A value that might be nil must be checked first, and nil itself can't go into a string. Text is for people, and "nil" showing up in it is a bug.
The expression must fit on the line, and can't contain a comment or an assignment. #{} with nothing inside is an error.
Text blocks: """
Text of several lines, like SQL or a message, goes between """ and """. The text inside can be indented along with the code around it, and the indentation is taken off again:
const table = "links"
const sql = """
SELECT "title"
FROM #{table}
WHERE read = 0
"""
print(sql) # prints SELECT "title"
# prints FROM links
# prints WHERE read = 0Here is how the block above becomes text:
- The opening
"""ends its line, and the text starts on the next. - Every line loses the indentation the closing
"""has, here two spaces. A line that starts further left than that is an error; one that's further in, likeWHERE, keeps what's past it. - The line break before the closing
"""isn't part of the text, sosqlends with0. - The closing
"""starts its own line, and code may go on after it, as in""", as: [String])at the end of a call.
Escapes and #{...} work as in any string, and each #{...} ends on its line. A " on its own is plain text, which is handy for SQL like the "title" above.
One line of text always goes in "...": """one line""" is an error.
Joining strings
+ joins two strings. Joining a string with anything else, such as an Int, is an error; put the value in with interpolation instead:
const age = 36
print("foo" + "bar") # prints foobar
print("age: " + age) # error: can't apply '+' to String and Int
print("age: #{age}") # prints age: 36String length
.length gives the number of characters in a string: "blåbær".length is 6. It is a value, so it has no parentheses (see parentheses). The methods strings have, such as upcase(), are listed with the other built-in methods.