String Functions
Texts are everywhere on Emma's farm: names of crops, signs at the market, notes from Grandpa Joe. In programming, a text is called a string, because it's a string of characters, one after another. In this lesson, you'll learn to work with strings: change them, cut them into parts, search them, and put them together neatly.
Lua keeps its functions for strings in a table called string. string.upper gives a text in capital letters, string.lower in small letters, and string.len gives its length, the same number as #:
local crop = "Pumpkin"
print(string.upper(crop))
print(string.lower(crop))
print(string.len(crop), #crop)This prints:
PUMPKIN
pumpkin
7 7There's a shorter way to write the same thing: crop:upper(). The colon means “take the function upper from string and give it crop as the first argument”, so crop:upper() is a short way of writing string.upper(crop). A function called with a colon is a method. Most Lua programmers call string functions this way, and so will this course. (How the colon works, and how to write methods of your own, you'll learn in the next lesson.)
local crop = "Pumpkin"
print(crop:upper(), crop:lower(), crop:len())This prints:
PUMPKIN pumpkin 7A method works on a text written straight in the code too, but the text must be in parentheses: ("pumpkin"):upper(). Without them, it's a syntax error:
print("pumpkin":upper())main.lua:1: ')' expected near ':'Two more typical mistakes:
- A dot instead of the colon.
crop.upper()callsupperwithout giving it the text, and the program ends withbad argument #1 to 'upper' (string expected, got no value): the first argument, the text, is missing. The colon would have passed it. - Forgetting the parentheses.
print(crop:upper)is the syntax errorfunction arguments expected near ')'.
String functions never change the text you give them. In fact, a string in Lua can't be changed at all: the functions give you a new text. A line with only crop:upper() makes the new text and throws it away. To keep it, store it:
local crop = "pumpkin"
crop:upper()
print(crop)
crop = crop:upper()
print(crop)This prints:
pumpkin
PUMPKINTwo more functions: rep repeats a text a number of times, and if you give it a second argument, puts it between the copies. reverse turns a text around:
local eggs = 7
print("Eggs: " .. ("#"):rep(eggs))
print(("ha"):rep(3, " "))
print(("stressed"):reverse())This prints:
Eggs: #######
ha ha ha
dessertsOne warning ahead: letters with accents, such as é or ñ, need extra care. ("purée"):upper() gives PURéE, and #"purée" is 6, not 5. Why, and what to do about it, you'll learn later in this lesson. Until then, the exercises count and change only words without accents.
Instructions
Emma wants people to read the sign from far away. Change the first print so that it prints the sign in capital letters, with the method upper:
STRAWBERRIES FOR 4 COINSPut the sign in a frame: print a line of = characters above it and another one below it, exactly as long as the sign. Make the line with rep and the length of the sign, not by typing the characters. The output then starts like this:
========================
STRAWBERRIES FOR 4 COINS
========================Grandpa Joe loves riddles: he wrote the sign for his eggs backwards. Below his riddle, print it turned around with reverse: Fresh Eggs.
Start programming for free
1/9