Lua strings are immutable byte sequences. They may contain ordinary text, binary data, and zero bytes. An operation never modifies an existing string; it returns a new one.
1 | local original = "one string" |
Literals and concatenation
Single- and double-quoted literals support escapes such as \n, \t, \\, and numeric byte escapes. Long brackets are convenient for multiline text:
1 | local document = [[ |
Concatenating many fragments in a loop repeatedly creates intermediate strings. Collect fragments in a table and use table.concat(parts) for large output.
Length and conversion
1 | print(#"Lua") -- 3 bytes |
Arithmetic may coerce numeric strings in some Lua versions, but explicit tonumber is clearer and more portable. The length operator counts bytes, not Unicode characters.
Standard operations
1 | local text = "Hello Lua" |
Other core functions include string.byte, string.char, string.rep, string.reverse, string.match, string.gmatch, and string.gsub.
Lua 5.3 and later include utf8 helpers such as utf8.len, utf8.codes, and utf8.char. They operate on UTF-8 code points, which are still not always the same as user-perceived characters. Use a Unicode library for normalization, locale-aware case conversion, or grapheme-cluster editing.