string — string functions
Lua's string library. Every function can also be called as a method on a string, so s:upper() is
the same as string.upper(s).
find, match, gsub and gmatch use Lua patterns (cheat sheet at the bottom). The
regex* functions take a .NET regular expression instead, which supports alternation,
repetition counts and the rest of the usual regex syntax.
| Function | Purpose |
|---|---|
string.byte |
Character codes of a substring |
string.char |
String from character codes |
string.dump |
Not supported (see below) |
string.find |
Find a pattern |
string.format |
Build a formatted string |
string.gmatch |
Iterate over all matches |
string.gsub |
Replace matches |
string.len |
Length in bytes |
string.lower / string.upper |
Change case |
string.match |
Extract captures |
string.regexMatch |
.NET regex match |
string.regexFind |
.NET regex find |
string.regexGsub |
.NET regex replace |
string.regexGmatch |
.NET regex iterator |
string.regexSplit |
.NET regex split |
string.rep |
Repeat a string |
string.reverse |
Reverse a string |
string.split |
Split a string into a list |
string.sub |
Extract a substring |
Lua 5.2 has no string.pack, string.unpack or string.packsize (those are 5.3+).
string.byte(s [, i [, j]])
Returns the numeric byte values of s from position i to j. i defaults to 1, j to i.
Negative positions count from the end.
print(string.byte("A")) -- 65
print(string.byte("ABC", 1, 3)) -- 65 66 67string.char(...)
Returns a string made of the given byte values.
print(string.char(65, 66, 67)) -- ABCstring.dump(function)
Present in the library but not supported by URLCheck's Lua implementation: calling it raises
stirng.dump is not supported. Do not use it.
string.find(s, pattern [, init [, plain]])
Looks for the first match of pattern in s, starting at init (default 1). Returns the start
and end positions, plus any captures; returns nil when there is no match. With plain = true the
pattern is matched literally.
print(string.find("hello world", "wor")) -- 7 9
print(string.find("a.b", ".", 1, true)) -- 2 2string.format(formatstring, ...)
Builds a string from a printf-style format. Common specifiers: %s (string), %d/%i (integer),
%f (fixed), %e/%g (scientific/shortest), %x/%X (hex), %o (octal), %c (character),
%q (quoted, escaped string) and %% (a literal %).
print(string.format("%s=%d (%.2f)", "page", 3, 1.5)) -- page=3 (1.50)
print(string.format("%q", 'a "quoted" line')) -- "a \"quoted\" line"string.gmatch(s, pattern)
Returns an iterator over every match of pattern in s. Captures are returned as separate values.
for name, value in string.gmatch("a=1&b=2", "([^&=]+)=([^&]*)") do
print(name, value)
endstring.gsub(s, pattern, repl [, n])
Replaces matches of pattern in s and returns the new string plus the number of replacements.
repl can be:
- a string (
%1,%2, … insert captures;%0inserts the whole match), - a table (looked up by the first capture),
- a function (called with the captures; return
nilto keep the match).
n limits how many replacements are made.
print(string.gsub("hello world", "o", "0")) -- hell0 w0rld 2
print(string.gsub("a=1", "(%w+)=(%w+)", "%2=%1")) -- 1=a 1
print(string.gsub("a b", "%s", function() return "_" end))string.len(s)
Returns the length of s in bytes. The # operator does the same.
print(string.len("abc")) -- 3string.lower(s) / string.upper(s)
Returns s with all letters converted to lower or upper case.
print(("HeLLo"):lower()) -- hello
print(("hello"):upper()) -- HELLOstring.match(s, pattern [, init])
Returns the captures of the first match of pattern in s, or the whole match when the pattern has
no captures. Returns nil when there is no match. init sets where to start.
print(string.match("page=42", "page=(%d+)")) -- 42
print(string.match("abc", "b")) -- bstring.rep(s, n [, sep])
Returns s repeated n times, with sep between the copies.
print(string.rep("ab", 3)) -- ababab
print(string.rep("ab", 3, "-")) -- ab-ab-abstring.reverse(s)
Returns s with its bytes in reverse order.
print(string.reverse("abc")) -- cbastring.sub(s, i [, j])
Returns the substring of s from i to j. j defaults to -1 (the end); negative positions
count from the end.
print(string.sub("hello", 2, 4)) -- ell
print(string.sub("hello", -3)) -- llostring.split(s [, sep])
Splits s on the literal string sep and returns the parts as a list (a table). Without sep —
or with an empty one — it splits on whitespace and drops empty pieces. The separator is literal;
use string.gmatch when you need a pattern.
print(table.concat(("a,b,c"):split(","), "|")) -- a|b|c
print(table.concat(("a,,b"):split(","), "|")) -- a||b
print(table.concat((" a b "):split(), "|")) -- a|bString metatable extensions
URLCheck adds two conveniences on top of the standard string metatable. Everything else — methods
such as s:sub(1, 1) and s:upper(), the .. operator, #s — works exactly as before.
Indexing. s[i] returns the character at position i, the same as string.sub(s, i, i).
Negative positions count from the end, and anything out of range is nil:
local s = "abc"
print(s[1], s[3], s[-1], s[4]) -- a c c nil+ concatenation. a + b concatenates when normal arithmetic cannot: "a" + "b" is "ab"
and "a" + 1 is "a1".
print("hello" + " " + "world") -- hello worldOne caveat: Lua's string-to-number coercion still wins first, so "1" + "2" is 3, not "12".
Use .. (or a non-numeric string) when you really mean concatenation.
Regex functions
Lua patterns are limited — no alternation, no {2,4} counts. These functions take a .NET regular
expression instead and are otherwise shaped like their Lua counterparts. Write the pattern as a
normal string ("\\d+") or as a long string ([[\d+]]), which avoids the escaping.
| Function | Like | What it does |
|---|---|---|
string.regexMatch(s, pattern [, init]) |
string.match |
Captures, or the whole match |
string.regexFind(s, pattern [, init]) |
string.find |
Start and end positions, then captures |
string.regexGsub(s, pattern, repl [, n]) |
string.gsub |
Replace matches |
string.regexGmatch(s, pattern) |
string.gmatch |
Iterator over the matches |
string.regexSplit(s, pattern) |
— | Split into a list |
("2026-10-30"):regexMatch("(\\d+)-(\\d+)-(\\d+)") -- 2026 10 30
("cat"):regexMatch("^(cat|dog)$") -- cat
("a1b22"):regexGsub("\\d+", "<$0>") -- a<1>b<22> 2
("a1b22"):regexGsub("\\d+", { ["1"] = "one" }) -- aoneb22 2
("a, b ,c"):regexSplit("\\s*,\\s*") -- { "a", "b", "c" }initis 1-based and counts from the end when negative, likestring.find.- Replacement strings use .NET syntax:
$0is the whole match,$1a group,${name}a named group. A table replacement is looked up by the whole match (or by the first capture). - Each pattern gets one second; a pattern that backtracks longer than that is stopped.
- An invalid pattern raises an error.
Lua patterns
Patterns look like regular expressions but are simpler. These are the pieces you will use most:
| Pattern | Matches |
|---|---|
. |
Any character |
%a |
A letter |
%d |
A digit |
%s |
A space character |
%w |
A letter or digit |
%l / %u |
A lower-case / upper-case letter |
%x |
A hexadecimal digit |
%p |
Punctuation |
%c |
A control character |
%% |
A literal % |
[abc] / [^abc] |
Any / none of the listed characters |
[a-z] |
A character in the range |
*, +, -, ? |
Zero or more, one or more, zero or more (shortest), optional |
^, $ |
Start / end of the subject |
( ) |
A capture |
%1 … %9 |
A back-reference to an earlier capture |
% + magic char |
The literal character, for example %. or %- |
print(("2026-10-30"):match("(%d+)-(%d+)-(%d+)")) -- 2026 10 30
print((" hi "):gsub("^%s+", ""):gsub("%s+$", "")) -- hi