Strings are a sequence of characters of any length that can include letters, numbers, symbols, and spaces.
In Lua, a string is a sequence of characters surrounded by a pair of single quotes
'' or double quotes
location = "Central Park" -- "Central Park" is a string
Multiple strings can be combined together using the concatenation operator
time = 10location = "Central Park"print("We will arrive at " .. location .. " at ".. time)
Escape characters are used for clarity and conciseness. Sometimes, Lua may interpret a character in a string, which can cause errors. To avoid this, escape characters can be used to clarify that a character should be read as a string.
brokenString = "They said, "Hello!"" -- This will cause "Hello!" to be outside of the stringrevisedString = "They said, \"Hello!\"" -- This will keep "Hello!" inside of the string using escape characters
The following is a list of some useful escape characters supported by Lua strings:
|quotation mark [double quote]||
|apostrophe [single quote]||
Lua’s string library contains many different string functions that can be used to manipulate strings. These functions include the following:
- Returns the ASCII equivalent of a given character.
- Converts a decimal value to its character representation.
- Creates formatted strings.
- Returns a provided string in reverse order.
- Extracts a substring from a given string.
- Converts all letters in a string to uppercase.