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
slocated at positioni. - string.byte(s, i, j)
- Return the numerical codes for the characters of string
slocated from positionithrough 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
patternwithinsand return the indices of the start and end of the first match, followed by the captured text, if any. Ifpatternis not found, return nil. - string.find (s, pattern, i)
- Search for
patternwithins, 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. Ifpatternis not found, return nil. - string.find (s, pattern, i, true)
- Looks for the first match of
patternwithins, starting from indexi(which can be negative), wherepatternis 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
spopulated with arguments.... See format strings for more information. - string.gmatch (s, pattern)
- Returns an iterator function that returns captures of
patternwithin strings. Ifpatternspecifies no captures, then the full match is returned. - string.gsub (s, pattern, repl)
- Returns a copy of string
swith all occurrences ofpatternreplaced 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
swith the firstnoccurrences ofpatternreplaced 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
swith characters changed to lower-case. - string.match (s, pattern)
- Matches
patternto stringsand returns any captures, or nil if there were no matches. Ifpatternspecified no captures, then return the full match. - string.match (s, pattern, i)
- Matches
patternto strings, starting from indexi(which may be negative) and returns any captures, or nil if there were no matches. Ifpatternspecified no captures, then return the full match. - string.rep (s, n)
- Returns a string that contains
ncopies ofs. - string.reverse (s)
- Returns a copy of string
swith characters in reverse order. - string.sub (s, i)
- Returns the sub-string of
scontaining characters starting from indexi(which may be negative) and continuing until the end ofs. - string.sub (s, i, j)
- Returns the sub-string of
scontaining characters at indicesithroughj, whereiandjmay be negative. - string.upper (s)
- Returns a copy of string
swith characters changed to upper-case.