string
The complete string library. Patterns in find/match/gmatch/gsub
use Lua patterns, not regex.
Inspection
Section titled “Inspection”string.len(s)
Section titled “string.len(s)”Byte length (not character count — see utf8.len for that).
print(string.len("portal")) --> 6print(string.len("pozo")) --> 4 (each ö is 2 bytes)string.byte(s, i?, j?)
Section titled “string.byte(s, i?, j?)”Numeric codes of bytes i..j (defaults: first byte only).
print(string.byte("A")) --> 65print(string.byte("ABCD", 2)) --> 66print(string.byte("ABCD", 1, 3)) --> 65 66 67string.char(...)
Section titled “string.char(...)”Builds a string from byte codes.
print(string.char(112, 111)) --> postring.find(s, pattern, init?, plain?)
Section titled “string.find(s, pattern, init?, plain?)”Returns start index, end index, plus captures. Pass plain=true for
literal searches.
print(string.find("hello portal", "portal")) --> 7 12print(string.find("a+b", "+", 1, true)) --> 2 2 (literal)local s, e, cap = string.find("hp=100", "(%d+)")print(s, e, cap) --> 4 6 100string.match(s, pattern, init?)
Section titled “string.match(s, pattern, init?)”Returns the captures (or the whole match if there are none).
print(string.match("Portal 1 v400", "(%d+)")) --> 1print(string.match("hp=100", "(%w+)=(%d+)")) --> hp 100string.gmatch(s, pattern)
Section titled “string.gmatch(s, pattern)”Iterator over all matches.
for word in string.gmatch("the cake is a lie", "%a+") do print(word)end--> the / cake / is / a / lieTransformation
Section titled “Transformation”string.sub(s, i, j?)
Section titled “string.sub(s, i, j?)”Substring; negative indices count from the end, j defaults -1.
print(string.sub("portal", 1, 3)) --> porprint(string.sub("portal", -3)) --> talstring.rep(s, n, sep?)
Section titled “string.rep(s, n, sep?)”Repetition, optionally joined by sep.
print(string.rep("na", 4)) --> nanananaprint(string.rep("x", 3, "-")) --> x-x-xstring.reverse(s)
Section titled “string.reverse(s)”print(string.reverse("portal")) --> latropstring.lower(s), string.upper(s)
Section titled “string.lower(s), string.upper(s)”ASCII case conversion.
print(string.upper("portal")) --> PORTALprint(string.lower("GLaDOS")) --> gladosstring.format(f, ...)
Section titled “string.format(f, ...)”Printf-style formatting: %s %d %f %.2f %x %X %o %c %q %%, with width
and precision (%5.2f).
print(string.format("%s has %d hp", "cube", 100)) --> cube has 100 hpprint(string.format("%.2f%%", 99.456)) --> 99.46%print(string.format("%5d|", 42)) --> 42|print(string.format("%x", 255)) --> ffprint(string.format("%q", 'a"b')) --> "a\"b"string.gsub(s, pattern, repl, n?)
Section titled “string.gsub(s, pattern, repl, n?)”Global substitution; repl may be a string (with %1 captures), a table,
or a function. Returns the new string and the count.
print(string.gsub("portal", "o", "0")) --> p0rtal 1print(string.gsub("hp=100", "(%d+)", "<%1>")) --> hp=<100> 1print(string.gsub("a b c", "%a+", {a="x", b="y"})) --> x y c 3print(string.gsub("one two", "%a+", function(w) return w:upper()end)) --> ONE TWO 2string.split(s, sep?) (Luau)
Section titled “string.split(s, sep?) (Luau)”Splits into a table of substrings. sep defaults to ","; empty fields
are preserved positionally.
local parts = string.split("id,name,version", ",")print(parts[2]) --> nameprint(#parts) --> 3print(string.split("a,,c")[2]) --> "" (empty, but present)Binary packing (Luau)
Section titled “Binary packing (Luau)”string.packsize(fmt)
Section titled “string.packsize(fmt)”Size in bytes a pack with this format will produce.
print(string.packsize("I4")) --> 4print(string.packsize("I2I4")) --> 6string.pack(fmt, ...)
Section titled “string.pack(fmt, ...)”Packs values into a binary string. Formats: b/B (i8/u8), h/H (i16/
u16), i/I[n] (integers), f (f32), d (f64), s (NUL-terminated),
</> (endianness, default little).
local blob = string.pack("<I4 f", 400, 0.5)print(#blob) --> 8string.unpack(fmt, s, pos?)
Section titled “string.unpack(fmt, s, pos?)”Unpacks values from a binary string; returns values followed by the next read position.
local blob = string.pack("<I2 s1", 7, "po")local n, str = string.unpack("<I2 s1", blob)print(n, str) --> 7 po