PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Aug 10 2018, 11:25 AM PST 2 min

​The string library provides functions for manipulating strings in Lua.


Functions


string.byte

int string.byte(string s, number i = 1, number j = i)

Description: Returns the internal numerical codes of the characters s[i], s[i+1], ..., s[j]. The default value for i is 1; the default value for j is i. These indices are corrected following the same rules of function string.sub.


string.char

string string.char(int ...)

Description: Receives zero or more integers. Returns a string with length equal to the number of arguments, in which each character has the internal numerical code equal to its corresponding argument.


string.dump

string string.dump(function f)

Description: Returns a string containing a binary representation of the given function, so that a later load on this string returns a copy of the function (but with new upvalues).


string.find

number, number string.find(string s, string pattern, number init = 1, number plain = false)

Description: Looks for the first match of pattern in the string s. If it finds a match, then find returns the indices of s where this occurrence starts and ends; otherwise, it returns nil. A third, optional numerical argument init specifies where to start the search; its default value is 1 and can be negative. A value of true as a fourth, optional argument plain turns off the pattern matching facilities, so the function does a plain "find substring" operation, with no characters in the pattern being considered "magic". Note that if _plain_ is given, then _init_ must be given as well.


string.format

string string.format(string formatstring, string ...)

Description: Returns a formatted version of its variable number of arguments following the description given in its first argument (which must be a string).


string.gmatch

function string.gmatch(string s, string pattern)

Description: Returns an iterator function that, each time it is called, returns the next captures from pattern over the string s.


string.gsub

string string.gsub(string s, string pattern, Variant repl)

Description: Returns a copy of s in which all (or the first n, if given) occurrences of the pattern have been replaced by a replacement string specified by repl, which can be a string, a table, or a function. gsub also returns, as its second value, the total number of matches that occurred.


string.len

int string.len(string s)

Description: Receives a string and returns its length.


string.lower

string string.lower(string s)

Description: Receives a string and returns a copy of this string with all uppercase letters changed to lowercase.


string.match

string string.match(string s, string pattern, string init)

Description: Looks for the first match of pattern in the string s. If a match is found, it is returned; otherwise, it returns nil. A third, optional numerical argument init specifies where to start the search; its default value is 1 and can be negative.


string.rep

string string.rep(string s, int n, string sep)

Description: Returns a string that is the concatenation of n copies of the string s separated by the string sep.


string.reverse

string string.reverse(string s)

Description: Returns a string that is the string s reversed.


string.sub

string string.sub(string s, int i = 1, int j = -1)

Description: Returns the substring of s that starts at i and continues until j; i and j can be negative. If j is absent, then it is assumed to be equal to -1 (which is the same as the string length).


string.upper

string string.upper(string s)

Description: Receives a string and returns a copy of this string with all lowercase letters changed to uppercase. All other characters are left unchanged.