Lua Strings: Literals, Concatenation, Bytes and UTF-8
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.
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:
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
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
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.