PcoWSkbVqDnWTu_dm2ix
Collapse Sidebar

string

string

This library provides generic functions for string manipulation such as finding and extracting substrings, pattern matching, and more. It provides all of its functions inside the global string variable.

For practical details on these functions, see /articles/Format String|Formatting and Converting Strings and /articles/string patterns reference|String Patterns.

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

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 string.char ( int ... )

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.

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

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 string.format ( string formatstring, string ... )

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

function string.gmatch ( string s, string pattern )

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

string , number string.gsub ( string s, string pattern, Variant replacement, number replacements )

Short for global substitution. Returns a copy of s in which all (or the first n, if given) occurrences of the pattern are substituted (replaced) with the given replacement. The second value returned is the total number of substitutions made.

The replacement can be one of several types, each used differently to determine the actual string:

  • string: The pattern is replaced with the string directly
  • table: The string that matched the pattern is looked up in the table as a key, and the value (string) is what replaces it, if it exists.
  • function: Called with the string that matched the pattern, should return the string to replace the matched pattern.

An optional final argument can be provided which specifies the maximum number of substitutions to make (for example, stop after 2 replacements)

Various Examples

-- Simple replacement
string.gsub("I love tacos!", "tacos", "Roblox") --> I love Roblox! 1
-- Using a pattern (hint: %w+ matches whole words)
string.gsub("I like red!", "%w+", "word") --> word word word 3
-- Replacement table
string.gsub("I play Roblox.", "%w+", {I="Je", play="joue à"}) --> Je joue à Roblox. 3
-- Replacement function
string.gsub("I have 2 cats.", "%d+", function (n) return tonumber(n) * 12 end) --> I have 24 cats. 1
-- Replace only twice
string.gsub("aaa", "a", "b", 2) --> "bba", 2
int string.len ( string s )

Receives a string and returns its length.

string string.lower ( string s )

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

string string.match ( string s, string pattern, number init = 1 )

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 string.rep ( string s, int n )

Returns a string that is the concatenation of n copies of the string s.

string string.reverse ( string s )

Returns a string that is the string s reversed.

table string.split ( string s, string separator = , )

Splits a string into parts based on the defined separator character(s), returning a table of ordered results.

If an empty “slice” is located, that part will be returned as an empty string. For instance string.split("abc||def", "|") will return a table with three strings: "abc", "", and "def".

local values = input:split(",")
print(values[1], values[2], values[3])

Also note that whitespace from the original string will be preserved, for example string.split("abc _ def", "_") will honor the whitespace on both sides of the _ separator. By default, the separator character is , but you can specify an alternative character or series of characters.

Corner Cases

Empty String

"" --> ""

Empty Slices

"foo,,bar" --> "foo", "", "bar"
",foo" --> "", "foo"
"foo," --> "foo", ""
"," --> "", ""
",," --> "", "", ""

Whitespace Preserved

"   whitespace   " --> "   whitespace   "
"foo , bar" --> "foo ", " bar"

Invalid UTF-8

"\xFF" --> "\xFF"
"\xFD,\xFE" --> "\xFD", "\xFE"

Unicode

"," --> U+FF0C FULLWIDTH COMMA
"我很高兴,你呢?" --> "我很高兴", "你呢?"
"•" --> U+2022 BULLET
"hello•world" --> "hello", "world"
string string.sub ( string s, int i = 1, int j = -1 )

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 string.upper ( string s )

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