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)Hello AdaPutting values into text
Section titled “Putting values into text”#{...} 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 = 3print("I have #{apples} apples, or #{apples * 2} halves.")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}")Ada LovelaceAda LovelaceSpecial characters
Section titled “Special characters”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}")She said "hi"one twofirst linesecond linea 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.
Longer text
Section titled “Longer text”Text that spans several lines goes between triple double quotes:
const poem = """ Roses are red, violets are blue. """print(poem)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.
Text taken exactly as written
Section titled “Text taken exactly as written”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')C:\new\folderno #{name} hereIn double quotes, \n in that path would have become a new line.
Characters
Section titled “Characters”A string is a sequence of characters, and count says how many:
const word = "café"print(word.count)print(word[0], word[3])4c é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..])EmeraldAsking for a position the string doesn’t have is an error that says which positions it does have:
print("cat"[5])text.em:1:7: index 5 is outside this String, which has 3 characters print("cat"[5]) ^^^^^^^^Valid indices are 0 through 2.Strings don’t change
Section titled “Strings don’t change”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"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".
Everyday things to do with text
Section titled “Everyday things to do with text”const line = " Hello, World "print(line.trim()) # remove spaces from both endsprint(line.trim().upper()) # capitalsprint(line.contains?("World")) # does it appear?print("a,b,c".split(",")) # break into a listprint("cat".replace("c", "b")) # swap one piece for anotherprint("ha".repeat(3)) # repeatHello, WorldHELLO, WORLDtrue["a", "b", "c"]bathahahaMethods 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.
Comparing and converting
Section titled “Comparing and converting”== compares two strings exactly, capitals included, and < compares them alphabetically:
print("Ada" == "Ada", "a" == "A", "apple" < "banana")true false trueTo 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)42!43What you’ve learned
Section titled “What you’ve learned”- A string is text in double quotes;
#{...}puts a value into it, and+joins strings. - Escapes such as
\nand\"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, andto_floatconvert.
Next, booleans and comparison: true, false, and questions a program can ask.