Strings
What a string is, how to join strings end to end, how to take part of a string, and how to build a new string one piece at a time.
1What a string is
A string is an ordered sequence of characters, written in double quotes: "New York". Letters, digits, punctuation, and spaces are all characters. Like a list, a string has a length and its characters have positions counted from 1.
city ← "New York" DISPLAY(LENGTH(city)) DISPLAY(city[5])
This displays 8, because the space counts, and then Y: character 4 is the space, so the Y is character 5.
The reference language has no string operators of its own. Programs work with strings through procedures, named commands that return a value. This guide uses two common ones:
| Procedure | Returns |
|---|---|
concat(str1, str2) | a new string: the characters of str1 followed by the characters of str2 |
substring(str, start, length) | a new string: length characters of str, starting at position start |
Different programs may name them differently. Always read how the procedure is described before using it.
2Joining strings
String concatenation joins two or more strings end to end to make a new string.
| Expression | Value | Why |
|---|---|---|
concat("Sun", "day") | "Sunday" | end to end, in order |
concat("day", "Sun") | "daySun" | swap the order, get a different string |
concat("5", "3") | "53" | digits in quotes are characters, not numbers |
concat("Mia", "Park") | "MiaPark" | no space unless you join one in |
concat("", "go") | "go" | the empty string changes nothing |
To join more than two strings, nest the calls: concat(concat("Mia", " "), "Park") is "Mia Park". Joining never does arithmetic: concat("5", "3") is "53", while the number expression 5 + 3 is 8.
3Taking part of a string
A substring is part of an existing string.
word ← "notebook" part ← substring(word, 5, 4) DISPLAY(part) DISPLAY(word) DISPLAY(LENGTH(word))
The program displays book, then notebook, then 8: the original string is unchanged and still 8 characters long. substring("notebook", 1, 4) is "note". Asking for characters past the end, such as 4 characters starting at position 6, is an error, just like a list index that is too large.
A string is an ordered sequence of characters, and spaces count. Concatenation joins strings end to end, in order, into a new string. A substring is a copy of part of an existing string; the original stays the same.
4Building a string in a loop
Start with the empty string "" and join on one piece each pass. Which side you join on decides the order of the result. This loop puts each new letter in front, so it reverses the word:
word ← "stop"
result ← ""
FOR EACH letter IN word
{
result ← concat(letter, result)
}
DISPLAY(result)
| letter | result after the pass |
|---|---|
"s" | "s" |
"t" | "ts" |
"o" | "ots" |
"p" | "pots" |
It displays pots. Change the line to result ← concat(result, letter) and it would display stop again.
5Worked examples
Make a username
What is displayed?
first ← "Jordan" last ← "Rivera" user ← concat(substring(first, 1, 1), last) DISPLAY(user)
- 1Work inside out.
substring(first, 1, 1)is the first character ofJordan:J. - 2Then
concat("J", last)joins it to the front ofRivera.
JRiveraTwo pieces of one word
What is displayed?
s ← "MIDNIGHT" DISPLAY(substring(s, 4, 5)) DISPLAY(substring(s, 1, 3))
- 1Number the characters: M1 I2 D3 N4 I5 G6 H7 T8.
- 2Start at 4 and take 5: N, I, G, H, T, which is
NIGHT. - 3Start at 1 and take 3:
MID.
NIGHT then MID.Digits as text
What is displayed?
a ← "7" b ← "11" c ← concat(a, b) d ← concat(b, a) DISPLAY(c) DISPLAY(d)
- 1
aandbare strings, soconcatjoins characters; nothing is added. - 2
cis "711" anddis "117": order matters.
711 then 117, not 18.Initials
What does this display?
phrase ← "keep it simple" initials ← "" initials ← concat(initials, substring(phrase, 1, 1)) initials ← concat(initials, substring(phrase, 6, 1)) initials ← concat(initials, substring(phrase, 9, 1)) DISPLAY(initials)
- 1Characters 1, 6, and 9 of
keep it simpleare the first letters of its three words: k, i, s. (The spaces are characters 5 and 8.) - 2Each line joins one letter to the end of
initials, which started empty.
kis6Common mistakes
concat({q('Mia')}, {q('Park')}) is {q('MiaPark')}. If you want a space, join one in.
{q('7')} is a character. Joining it to {q('11')} gives {q('711')}, never 18.
Every space has a position and adds 1 to the length. Write out the positions under the string when it matters.
A substring is a new string made from part of the old one. The old string keeps every character.
“substring(first, 1, 1) gives the first character, J. concat joins it end to end with Rivera, so the result is JRivera.”
Evaluate the innermost call first, name each piece, and say in which order the pieces are joined.