Strings Tutorial |
|
Strings are introduced in section 2.1 of the Reference Manual [1]. Strings can be defined using single quotes, double quotes, or double square brackets.
> = "hello" hello > = 'hello' hello > = [[hello]] hello
Why so many ways to make a string? It allows you to enclose one type of quotes in the other. e.g.,
> = 'hello "Lua user"' hello "Lua user" > = "Its [[content]] hasn't got a substring." Its [[content]] hasn't got a substring. > = [[Let's have more "strings" please.]] Let's have more "strings" please.
Double bracketed strings also have a few other special properties, discussed below.
Lua can also handle C-like escape sequences. There are more details in the Reference Manual, section 2.1 [1].
> = "hello \"Lua user\"" hello "Lua user" > = 'hello\nNew line\tTab' hello New line Tab
Escape sequences are not recognized when using double brackets, so:
> = [[hello\nNew line\tTab]]
hello\nNew line\tTab
Double square brackets can be used to enclose literal strings which traverse several lines. e.g.,
> = [[Multiple lines of text >> can be enclosed in double square >> brackets.]] Multiple lines of text can be enclosed in double square brackets.
Double square brackets allow nesting, but they require one or more = inserted in the outer-most brackets to distinguish them. It doesn't matter how many = are inserted, as long as the number is the same in the beginning and ending brackets.
> = [[one [[two]] one]] -- bad stdin:1: nesting of [[...]] is deprecated near '[' > = [=[one [[two]] one]=] -- ok one [[two]] one > = [===[one [[two]] one]===] -- ok too one [[two]] one > = [=[one [ [==[ one]=] -- ok. nothing special about the inner content. one [ [==[ one
Strings can be joined together using the concatenation operator "..". e.g.,
> = "hello" .. " Lua user" hello Lua user > who = "Lua user" > = "hello "..who hello Lua user
> = "Green bottles: "..10 Green bottles: 10 > = type("Green bottles: "..10) string
Note that concatenating large strings can take longer than adding them to an array and accessing them individually, later. The following Lua 4 code demonstrates how this is done (but may not be the best example of speed improvement):
newtable = {}
function insertmany(ttable, ...)
for var i = 1, getn(arg) do
tinsert(ttable, arg[i])
end
end
insertmany(newtable, 'Hello world! ', 'The first time ', 'is always ', 'the best. ', 'Never ', ' say ', 'never ', 'again.')
for var i, getn(newtable) do
write(newtable[i])
end
Lua supplies a range of useful functions for processing and manipulating strings in its standard library. More details are supplied in the StringLibraryTutorial. Below are a few examples of usage of the string library.
> = string.byte("ABCDE", 2) -- return the ASCII value of the second character 66 > = string.char(65,66,67,68,69) -- return a string constructed from ASCII values ABCDE > = string.find("hello Lua user", "Lua") -- find substring "Lua" 7 9 > = string.find("hello Lua user", "l+") -- find one or more occurrences of "l" 3 4 > = string.format("%.7f", math.pi) -- format a number 3.1415927 > = string.format("%8s", "Lua") -- format a string Lua
Lua performs automatic conversion of numbers to strings and vice versa where it is appropriate. This is called coercion.
> = "This is Lua version " .. 5.1 .. " we are using." This is Lua version 5.1 we are using. > = "Pi = " .. math.pi Pi = 3.1415926535898 > = "Pi = " .. 3.1415927 Pi = 3.1415927
string.format() function. e.g.,
> = string.format("%.3f", 5.1) 5.100 > = "Lua version " .. string.format("%.1f", 5.1) Lua version 5.1