The Escape Character
Strings are defined by wrapping text in single or double quotes. We already know this.
local output = "Hello world!"
local output = 'Hello world!'
And this works fine because if the string itself required a quote, there was still the unused single/double variant to specify the string.
local output = '"There are two ways to write error-free programs; only the third one works." said Alan.'
local output = "It's taco time!"
But what happens when a string requires the use of both single and double quotes?
local output = '"Hello world!" was displayed on the machine's output.'
print(output)--Error: Incomplete statement: expected assignment or a function call
The apostrophe closes out the string, leaving the fragment “‘s display” hanging out there like a pair of forgotten socks.
Sure, we could switch the outer single quotes to double quotes and switch the inner double quotes to single quotes and do so any time we have to cross this bridge.
Or we could just come up with a less clumsy solution.
Yes. Let’s do that instead.
We’ll sacrifice a character we don’t like (or use) very much and use that to indicate to the interpreter that ‘for this next character, treat it literally as-is and not an internal symbol.’
In C-programming (and Lua), this is called the escape character and designated by the backslash \
.
local output = '"Hello world!" could be seen on the machine\'s display.'
print(output) --"Hello world!" could be seen on the machine's display.
The escape character, together with some combination of characters form an ‘escape sequence’ that can also print out non-display characters such as tab or a new line.
(Check the PIL for a complete list.)
local output = "\t\t\t3.14"
print(output)
-- 3.14
local output = "Hello\nworld!"
print(output)
-- Hello
--world!
string.format
The concept of placeholders and format specifiers are not limited to just date strings.
You can insert your own placeholders in a string and use the ‘format’ function to replace said placeholders during runtime.
Previously, if we wanted to add runtime generated text to a string, we’d do it using the concatenation operator. This works but can get messy real quick.
local r1 = math.random(100)
local r2 = math.random(100)
local r3 = math.random(100)
local sum = r1 + r2 + r3
local output = "The sum of " .. r1 .. ", " .. r1 .. ", and " .. r3 .. " is " .. sum .. "."
print(output)
--The sum of 96, 96, and 96 is 289.
Instead, we can write the string ahead of time and use placeholders that will be filled in during runtime.
local r1 = math.random(100)
local r2 = math.random(100)
local r3 = math.random(100)
local sum = r1 + r2 + r3
local str = "The sum of %i, %i, and %i is %i."
local output = string.format(str, r1, r2, r3, sum)
print(output)
--The sum of 87, 68, and 69 is 224.
Placeholders are data type specific so you need to specify the proper format.
This example is hard-coded to accept four numbers. But you can also design it so that any number of input can be accepted.
Using the example above, extend it to accept and display any number of inputs during runtime.
For 2, 3, and 4 numbers, your output should look like this:
--The sum of 30 and 61 is 91.
--The sum of 38, 97, and 96 is 231.
--The sum of 64, 17, 2, 33, and 13 is 129.
string.split
When you retrieve data from a data store, it will likely be in the string format and separated by some designated character.
The ‘split’ function takes a string and returns a table with values separated by said character.
local fruitString = "apple, banana, peach"
local fruitList = string.split(fruitString)
for _, fruit in ipairs(fruitList) do
print(fruit)
end
--apple
-- banana
-- peach
By default, split uses comma as the separator. You can specify the character by providing a second argument to the function.
The example above does not remove the spaces. We can remove spaces by doing:
local fruitString = "apple, banana, peach"
local fruitList = string.split(fruitString, ', ')
for _, fruit in ipairs(fruitList) do
print(fruit)
end
--apple
--banana
--peach
string.sub
The ‘sub’ function returns part of a string, delineated by one or two numbers to indicate that span. The first number is the initial position and reads until the end of the string or until the position indicated by the second number if given.
local str = "Hello world!"
local substring = string.sub(str, 5)
print(substring) --o world!
local str = "Hello world!"
local substring = string.sub(str, 4, 8)
print(substring) --lo wo