Passer au contenu principal
Version: bleeding-edge 🩸

string

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.

👐Open Source
This library implementation is Open Sourced on GitHub!
🧑‍💻API Source
This page is auto-generated! The Functions, Properties and Events described here are defined in our GitHub's API Repository! Feel free to commit suggestions and changes to the source .json API files!

tip

You can use this library statically as string.method(my_str, ...) or directly as my_str:method(...)!

🎒 Examples

(string.StartsWith) Checks if string starts with text
local str = "awesome text!"
local starts = str:StartsWith("awesome")
-- Outputs "true"
(string.EndsWith) Checks if string ends with text
local str = "awesome text!"
local ends = str:EndsWith("text!")
-- Outputs "true"
(string.Trim) Removes extra spaces
local str = " Hello world! "
local new_str = str:Trim()
-- Outputs "Hello world!"
(string.FormatArgs) Replaces {1} and {2} with corresponding texts
local str = "Hello {2} I'm {1}"
local new_str = str:FormatArgs("a noob", "world!")
-- Outputs "Hello world! I'm a noob"
(string.ToTable) Convert string to table of characters
local str = "hello"
local tbl = str:ToTable()
-- Outputs "{'h', 'e', 'l', 'l', 'o'}"

🦠 Functions

ReturnsNameDescription
booleanStartsWithTest whether this string starts with given string
booleanEndsWithTest whether this string ends with given string
stringTrimReturns a new string with removed whitespace characters from the front and end of this string
stringFormatArgsA better string.Format, replace {num} by the corresponding vararg in a string
varargs of integerbyteReturns the internal numeric codes of the characters s[start_pos], s[start_pos + 1], ..., s[end_pos]
stringcharReceives zero or more integers. Returns a string with length equal to the number of arguments, in which each character has the internal numeric code equal to its corresponding argument
number, number, stringfindLooks for the first match of pattern in the string.
stringrepReturns a string that is the concatenation of n copies of the string s separated by the string sep.
stringlowerReturns a copy of this string with all uppercase letters changed to lowercase.
stringupperReturns a copy of this string with all lowercase letters changed to uppercase.
stringdumpReturns a string containing a binary representation (a *binary chunk*) of the given function.
numbermatchLooks for the first match of pattern in the string.
stringreverseReturns a string that is the string s reversed.
stringsubReturns the substring of the string that starts at i and continues until j.
functiongmatchReturns an iterator function that, each time it is called, returns the next captures from pattern over the string s.
numberlenReturns its length.
string, numbergsubReturns 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.
stringformatReturns a formatted version of its variable number of arguments following the description given in its first argument.
table of stringToTableSplit string into table of single characters

StartsWith

Test whether this string starts with given string
local ret = my_string:StartsWith(other_string)

Parameters

TypeParameterDefaultDescription
stringother_string Required parameter The given string

Returns

TypeDescription
booleanNo description provided
string.StartsWith Examples
Checks if string starts with text
local str = "awesome text!"
local starts = str:StartsWith("awesome")
-- Outputs "true"

EndsWith

Test whether this string ends with given string
local ret = my_string:EndsWith(other_string)

Parameters

TypeParameterDefaultDescription
stringother_string Required parameter The given string

Returns

TypeDescription
booleanNo description provided
string.EndsWith Examples
Checks if string ends with text
local str = "awesome text!"
local ends = str:EndsWith("text!")
-- Outputs "true"

Trim

Returns a new string with removed whitespace characters from the front and end of this string
local ret = my_string:Trim()

Returns

TypeDescription
stringNo description provided
string.Trim Examples
Removes extra spaces
local str = " Hello world! "
local new_str = str:Trim()
-- Outputs "Hello world!"

FormatArgs

A better string.Format, replace {num} by the corresponding vararg in a string
local ret = my_string:FormatArgs(args...)

Parameters

TypeParameterDefaultDescription
varargs of anyargs... Required parameter The args

Returns

TypeDescription
stringNo description provided
string.FormatArgs Examples
Replaces {1} and {2} with corresponding texts
local str = "Hello {2} I'm {1}"
local new_str = str:FormatArgs("a noob", "world!")
-- Outputs "Hello world! I'm a noob"

byte

Returns the internal numeric codes of the characters s[start_pos], s[start_pos + 1], ..., s[end_pos]
local ret_01, ret_02, ... = my_string:byte(start_pos?, end_pos?)

Parameters

TypeParameterDefaultDescription
integerstart_pos?1The first character of the string to get the byte of
integerend_pos?start_posThe last character of the string to get the byte of

Returns

TypeDescription
varargs of integerthe internal numeric code

char

Receives zero or more integers. Returns a string with length equal to the number of arguments, in which each character has the internal numeric code equal to its corresponding argument
local ret = my_string:char(values...)

Parameters

TypeParameterDefaultDescription
varargs of integervalues... Required parameter No description provided

Returns

TypeDescription
stringthe string

find

Looks for the first match of pattern in the string.
local ret_1, ret_2, ret_3 = my_string:find(haystack, needle, startPos, noPatterns)

Parameters

TypeParameterDefaultDescription
stringhaystack Required parameter The string to search in.
stringneedle Required parameter The string to find, can contain patterns if enabled.
numberstartPos Required parameter The position to start the search from, can be negative start position will be relative to the end position.
booleannoPatterns Required parameter Disable patterns.

Returns

TypeDescription
numberNo description provided
numberNo description provided
stringNo description provided

rep

Returns a string that is the concatenation of n copies of the string s separated by the string sep.
local ret = my_string:rep(str, repetitions, separator)

Parameters

TypeParameterDefaultDescription
stringstr Required parameter The string to convert.
numberrepetitions Required parameter Timer to repeat, this values gets rounded internally.
stringseparator Required parameter String that will separate the repeated piece. Notice that it doesn't add this string to the start or the end of the result, only between the repeated parts.

Returns

TypeDescription
stringNo description provided

lower

Returns a copy of this string with all uppercase letters changed to lowercase.
local ret = my_string:lower(str)

Parameters

TypeParameterDefaultDescription
stringstr Required parameter The string to convert.

Returns

TypeDescription
stringNo description provided

upper

Returns a copy of this string with all lowercase letters changed to uppercase.
local ret = my_string:upper(str)

Parameters

TypeParameterDefaultDescription
stringstr Required parameter The string to convert.

Returns

TypeDescription
stringNo description provided

dump

Returns a string containing a binary representation (a *binary chunk*) of the given function.
local ret = my_string:dump(func, stripDebugInfo)

Parameters

TypeParameterDefaultDescription
functionfunc Required parameter The function to get the bytecode of
booleanstripDebugInfo Required parameter True to strip the debug data, false to keep it

Returns

TypeDescription
stringNo description provided

match

Looks for the first match of pattern in the string.
local ret = my_string:match(string, pattern, startPosition)

Parameters

TypeParameterDefaultDescription
stringstring Required parameter String which should be searched in for matches.
stringpattern Required parameter The pattern that defines what should be matched.
numberstartPosition Required parameter The start index to start the matching from, can be negative to start the match from a position relative to the end.

Returns

TypeDescription
numberNo description provided

reverse

Returns a string that is the string s reversed.
local ret = my_string:reverse(str)

Parameters

TypeParameterDefaultDescription
stringstr Required parameter The string to be reversed.

Returns

TypeDescription
stringNo description provided

sub

Returns the substring of the string that starts at i and continues until j.
local ret = my_string:sub(string, StartPos, EndPos)

Parameters

TypeParameterDefaultDescription
stringstring Required parameter The string you'll take a sub-string out of.
numberStartPos Required parameter The position of the first character that will be included in the sub-string.
numberEndPos Required parameter The position of the last character to be included in the sub-string. It can be negative to count from the end.

Returns

TypeDescription
stringNo description provided

gmatch

Returns an iterator function that, each time it is called, returns the next captures from pattern over the string s.
local ret = my_string:gmatch(data, pattern)

Parameters

TypeParameterDefaultDescription
stringdata Required parameter The string to search in
stringpattern Required parameter The pattern to search for

Returns

TypeDescription
functionNo description provided

len

Returns its length.
local ret = my_string:len(str)

Parameters

TypeParameterDefaultDescription
stringstr Required parameter The string to find the length of.

Returns

TypeDescription
numberNo description provided

gsub

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.
local ret_1, ret_2 = my_string:gsub(string, pattern, replacement, maxReplaces)

Parameters

TypeParameterDefaultDescription
stringstring Required parameter String which should be modified.
stringpattern Required parameter The pattern that defines what should be matched and eventually be replaced.
stringreplacement Required parameter In case of a string the matched sequence will be replaced with it. In case of a table, the matched sequence will be used as key and the table will tested for the key, if a value exists it will be used as replacement. In case of a function all matches will be passed as parameters to the function, the return value(s) of the function will then be used as replacement.
numbermaxReplaces Required parameter Maximum number of replacements to be made.

Returns

TypeDescription
stringNo description provided
numberNo description provided

format

Returns a formatted version of its variable number of arguments following the description given in its first argument.
local ret = my_string:format(format, formatParameters)

Parameters

TypeParameterDefaultDescription
stringformat Required parameter The string to be formatted
numberformatParameters Required parameter Values to be formatted into the string.

Returns

TypeDescription
stringNo description provided

ToTable

Converts this string into a table where each element is a single-character substring of the original string.
local ret = my_string:ToTable(str)

Parameters

TypeParameterDefaultDescription
stringstr Required parameter The string to convert into a table

Returns

TypeDescription
table of stringTable of single-character strings
string.ToTable Examples
Convert string to table of characters
local str = "hello"
local tbl = str:ToTable()
-- Outputs "{'h', 'e', 'l', 'l', 'o'}"