String Manipulation

Strings in Lua are immutable and can be enclosed in either single or double quotes:

str1 = "Hello"
str2 = 'World'

Concatenation:

message = str1 .. " " .. str2
print(message) -- Hello World

Length of String:

print(#message) -- 11

Common String Functions:

string.upper("lua")       -- "LUA"
string.lower("LUA")       -- "lua"
string.len("hello")       -- 5
string.sub("hello", 2, 4) -- "ell"
string.find("hello", "l") -- 3
string.reverse("abc")     -- "cba"

Multiline Strings:

Use double square brackets:

text = [[
This is a
multiline string.
]]
print(text)
← PrevNext →