Skip to content


This library provides generic functions for string manipulation, such as finding and extracting substrings, and pattern matching. When indexing a string in Lua, the first character is at position 1 (not at 0, as in C). Indices are allowed to be negative and are interpreted as indexing backwards, from the end of the string. Thus, the last character is at position -1, and so on. Several functions in this library (e.g., find, gfind, gsub), make use of Lua patterns.



Returns true if a string ends with a given substring. This function does not use pattern matching.

local result = string.endswith(s, pattern)


  • s (string)
  • pattern (string)


  • result (boolean)


This function creates a string, given various values. The first parameter follows the printf format, with the additional option of %q to automatically quote a string.

Specifier Output Example
%d Signed decimal integer. -392
%i Same as %d. -392
%u Unsigned decimal integer. 7235
%o Unsigned octal. 610
%x Unsigned hexadecimal integer. 7fa
%X As %x, but uppercase. 7FA
%f Decimal floating point, lowercase. 392.65
%F Decimal floating point, uppercase. 392.65
%e Scientific notation (mantissa/exponent), lowercase. 3.9265e+2
%E Scientific notation (mantissa/exponent), uppercase. 3.9265E+2
%g Use the shortest representation: %e or %f. 392.65
%G Use the shortest representation: %E or %F. 392.65
%a Hexadecimal floating point, lowercase. -0xc.90fep-2
%A Hexadecimal floating point, uppercase. -0XC.90FEP-2
%c Character. a
%s String of characters. sample
%q Quoted string of characters. "sample"
%p Pointer address. b8000000
%% The literal % character. %
local result = string.format(format, ...)


  • format (string): The format string to use for the output.
  • ... (any): Optional. Values to format into the specified string.


  • result (string)


Returns a string where one string has been inserted into another, after a specified position.

For example, string.insert("12345678", "abcdefgh", 5) will return "12345abcdefgh678".

local result = string.insert(s1, s2, position)


  • s1 (string): The string to insert into.
  • s2 (string): The string to insert.
  • position (integer): An index of s1. The s2 string will be inserted after this index.


  • result (string): A copy of s1, with s2 inserted into it after the specified position.


Performs the logic of string.find on a string s, using a table of patterns.

If any of the patterns are found in s, then the matching pattern will be returned, followed by the normal results of string.find.

The patterns are checked in the order they are passed. i.e., this function will first try to match patterns[1], then patterns[2], and so on.

local pattern, startindex, endindex = string.multifind(s, patterns, index, plain)


  • s (string): The string to find patterns in.
  • patterns (table): An array-style table that contains the patterns to match.
  • index (integer): Default: 1. Start index of the find. (Same meaning as in string.find.)
  • plain (boolean): Default: false. If true, then a normal search will be performed instead of a pattern search. (Same meaning as in string.find.)


  • pattern (string): Optional. If a pattern was matched, then this will be the first pattern that was matched. If no patterns matched, this will be nil.
  • startindex (integer): Optional. If a pattern was matched, this is the index of s where the matching pattern begins.
  • endindex (integer): Optional. If a pattern was matched, this is the index of s where the matching pattern ends.


Returns an array-style table with a string split by a specified separator. The seperator is not part of the results. By default the sep == "%s", which will result in str getting split by whitespace characters (e.g. spaces and tabs).


if sep is more than one character, then str will get split at each occurrence of any of the individual characters in sep. For example, string.split("1a2b3c4abc5", "abc") will return {"1", "2", "3", "4", "5"} instead of {"1a2b3c4", "5"}.

local split = string.split(str, sep)


  • str (string): The string to split.
  • sep (string): Default: "%s". The token to split the string by.


  • split (string[])


Returns true if a string begins with a given substring. This function does not use pattern matching.

local result = string.startswith(s, substring)


  • s (string)
  • substring (string)


  • result (boolean)


Returns a copy of the string, with whitespace characters (e.g. spaces and tabs) removed from the start and end.

local trimmed = string.trim(s)


  • s (string)


  • trimmed (string)