Working with Lua's String Library

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 position i.
string.byte(s, i, j)
Return the numerical codes for the characters of string s located from position i through position j. 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 within s and return the indices of the start and end of the first match, followed by the captured text, if any. If pattern is not found, return nil.
string.find (s, pattern, i)
Search for pattern within s, starting from index i (which can be negative), and return the indices of the start and end of the first match, followed by the captured text, if any. If pattern is not found, return nil.
string.find (s, pattern, i, true)
Looks for the first match of pattern within s, starting from index i (which can be negative), where pattern 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 string s. If pattern specifies no captures, then the full match is returned.
string.gsub (s, pattern, repl)
Returns a copy of string s with all occurrences of pattern replaced by repl, 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 first n occurrences of pattern replaced by repl, 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 string s and returns any captures, or nil if there were no matches. If pattern specified no captures, then return the full match.
string.match (s, pattern, i)
Matches pattern to string s, starting from index i (which may be negative) and returns any captures, or nil if there were no matches. If pattern specified no captures, then return the full match.
string.rep (s, n)
Returns a string that contains n copies of s.
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 index i (which may be negative) and continuing until the end of s.
string.sub (s, i, j)
Returns the sub-string of s containing characters at indices i through j, where i and j may be negative.
string.upper (s)
Returns a copy of string s with characters changed to upper-case.