Skip to content

Text

A piece of text is a String. You have already written some: anything between double quotes is a string.

const greeting = "Hello"
const name = "Ada"
print(greeting, name)
Output
Hello Ada

#{...} inside a string puts a value into the text, as The First Program showed. Whatever is between #{ and } is worked out first, so it can be any expression:

const apples = 3
print("I have #{apples} apples, or #{apples * 2} halves.")
Output
I have 3 apples, or 6 halves.

Strings can also be joined with +, but only to other strings. Interpolation is usually easier to read, and it works with any value:

const first = "Ada"
const last = "Lovelace"
print(first + " " + last)
print("#{first} #{last}")
Output
Ada Lovelace
Ada Lovelace

Some characters can’t be typed straight into a string. A backslash starts an escape, a short code for one:

print("She said \"hi\"")
print("one\ttwo")
print("first line\nsecond line")
print("a backslash: \\")
print("not interpolated: \#{name}")
Output
She said "hi"
one two
first line
second line
a backslash: \
not interpolated: #{name}

\" is a double quote, \t a tab, \n a new line, \\ a backslash, and \# a # that doesn’t start an interpolation. \u{e9} writes a character by its Unicode number, which is handy for one that is hard to type or invisible, such as a combining accent.

Text that spans several lines goes between triple double quotes:

const poem = """
Roses are red,
violets are blue.
"""
print(poem)
Output
Roses are red,
violets are blue.

The text starts on the line after the opening """, and the closing """ goes on a line of its own. The indentation before the closing quotes is removed from every line, so the string can be indented to match your code while any extra indentation, like the second line’s, is kept. Interpolation and escapes work here too.

A string in single quotes is raw: backslashes and #{ are just characters. That suits text full of backslashes, such as a Windows path:

print('C:\new\folder')
print('no #{name} here')
Output
C:\new\folder
no #{name} here

In double quotes, \n in that path would have become a new line.

A string is a sequence of characters, and count says how many:

const word = "café"
print(word.count)
print(word[0], word[3])
Output
4
c é

word[0] is the first character; counting starts at 0, so the last one is word[count - 1]. Emerald counts characters the way a reader sees them, so é is one character, and so is an emoji built from several parts, such as a family 👨‍👩‍👧.

A range of positions gives part of a string. ..< stops before its end, and .. includes it:

const word = "Emerald"
print(word[0..<3])
print(word[3..])
Output
Eme
rald

Asking for a position the string doesn’t have is an error that says which positions it does have:

print("cat"[5])
Output
text.em:1:7: index 5 is outside this String, which has 3 characters
print("cat"[5])
^^^^^^^^
Valid indices are 0 through 2.

Once made, a string can’t be changed in place. Methods that seem to change one, like upper, give back a new string and leave the original as it was:

var word = "cat"
word[0] = "b"
Output
text.em:2:1: a String cannot be changed in place
word[0] = "b"
^^^^^^^
Strings are immutable. Build a new one instead, for example with `replace` or interpolation.

A var can still be given a whole new string: word = "bat".

const line = " Hello, World "
print(line.trim()) # remove spaces from both ends
print(line.trim().upper()) # capitals
print(line.contains?("World")) # does it appear?
print("a,b,c".split(",")) # break into a list
print("cat".replace("c", "b")) # swap one piece for another
print("ha".repeat(3)) # repeat
Output
Hello, World
HELLO, WORLD
true
["a", "b", "c"]
bat
hahaha

Methods can be chained, one after another, reading left to right: line.trim().upper() trims, then capitalizes. A method whose name ends in ?, like contains?, answers a yes-or-no question. The String page lists every method.

== compares two strings exactly, capitals included, and < compares them alphabetically:

print("Ada" == "Ada", "a" == "A", "apple" < "banana")
Output
true false true

To turn a number into text, interpolate it or call to_string(); to turn text into a number, call to_int() or to_float():

print(42.to_string() + "!")
print("42".to_int() + 1)
Output
42!
43
  • A string is text in double quotes; #{...} puts a value into it, and + joins strings.
  • Escapes such as \n and \" write special characters; triple quotes hold several lines; single quotes take text exactly as written.
  • count, [i], and ranges read the characters, counted as a reader sees them, from 0.
  • Strings never change; their methods give back new strings.
  • == and < compare text; to_string, to_int, and to_float convert.

Next, booleans and comparison: true, false, and questions a program can ask.