Lua's string library contains a collection of functions that perform a variety of string manipulations, from finding and extracting sub-strings to pattern matching.
Lua string indices start at 1, and can be negative in order to reference character relative to the
end of the string (meaning, index -1
refers to the last character, -2
is the character located
to the left of the last character, etc).
The string library is always available, and its functions can be accessed on the string module. Additionally, strings also support object-oriented access to the string functions as shown in the following example:
local example = "tui.ninja"
-- access on the string module
print(string.upper(example)) -- TUI.NINJA
-- called on the string itself
print(example:upper()) -- TUI.NINJA
The string library contains the following functions:
- string.byte(s)
- Return the numerical code for the first character of string
s
. - string.byte(s, i)
- Return the numerical code for the character of string
s
located at positioni
. - string.byte(s, i, j)
- Return the numerical codes for the characters of string
s
located from positioni
through positionj
. The numerical code(s) returned are not necessarily portable across platforms, but are generally ASCII codes. - string.char(i)
- Returns a string containing the character associated with the specified integer.
- string.char(i, ...)
- Receives 0 or more integers, then returns a string containing the characters corresponding to the specified integers. The numerical code(s) returned are not necessarily portable across platforms, but are generally ASCII codes.
- string.find (s, pattern)
- Search for
pattern
withins
and return the indices of the start and end of the first match, followed by the captured text, if any. Ifpattern
is not found, return nil. - string.find (s, pattern, i)
- Search for
pattern
withins
, starting from indexi
(which can be negative), and return the indices of the start and end of the first match, followed by the captured text, if any. Ifpattern
is not found, return nil. - string.find (s, pattern, i, true)
- Looks for the first match of
pattern
withins
, starting from indexi
(which can be negative), wherepattern
is treated as a literal string. If a match is found, return the start and end indices of the first match. - string.format (s, ยทยทยท)
- Returns format string
s
populated with arguments...
. See format strings for more information. - string.gmatch (s, pattern)
- Returns an iterator function that returns captures of
pattern
within strings
. Ifpattern
specifies no captures, then the full match is returned. - string.gsub (s, pattern, repl)
- Returns a copy of string
s
with all occurrences ofpattern
replaced byrepl
, which can be a string, table, or function, along with a second value representing the number of matches that occurred. See substitution for more information. - string.gsub (s, pattern, repl, n)
- Returns a copy of string
s
with the firstn
occurrences ofpattern
replaced byrepl
, which can be a string, table, or function, along with a second value representing the number of matches that occurred. See substitution for more information. - string.len (s)
- Returns the length of string
s
. - string.lower (s)
- Returns a copy of string
s
with characters changed to lower-case. - string.match (s, pattern)
- Matches
pattern
to strings
and returns any captures, or nil if there were no matches. Ifpattern
specified no captures, then return the full match. - string.match (s, pattern, i)
- Matches
pattern
to strings
, starting from indexi
(which may be negative) and returns any captures, or nil if there were no matches. Ifpattern
specified no captures, then return the full match. - string.rep (s, n)
- Returns a string that contains
n
copies ofs
. - string.reverse (s)
- Returns a copy of string
s
with characters in reverse order. - string.sub (s, i)
- Returns the sub-string of
s
containing characters starting from indexi
(which may be negative) and continuing until the end ofs
. - string.sub (s, i, j)
- Returns the sub-string of
s
containing characters at indicesi
throughj
, wherei
andj
may be negative. - string.upper (s)
- Returns a copy of string
s
with characters changed to upper-case.