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"
🦠 Functions
Returns | Name | Description | |
---|---|---|---|
boolean | StartsWith | Test whether this string starts with given string | |
boolean | EndsWith | Test whether this string ends with given string | |
string | Trim | Returns a new string with removed whitespace characters from the front and end of this string | |
string | FormatArgs | A better string.Format, replace {num} by the corresponding vararg in a string | |
varargs of integer | byte | Returns the internal numeric codes of the characters s[start_pos], s[start_pos + 1], ..., s[end_pos] | |
string | 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 | |
number, number, string | find | Looks for the first match of pattern in the string. | |
string | rep | Returns a string that is the concatenation of n copies of the string s separated by the string sep . | |
string | lower | Returns a copy of this string with all uppercase letters changed to lowercase. | |
string | upper | Returns a copy of this string with all lowercase letters changed to uppercase. | |
string | dump | Returns a string containing a binary representation (a *binary chunk*) of the given function. | |
number | match | Looks for the first match of pattern in the string. | |
string | reverse | Returns a string that is the string s reversed. | |
string | sub | Returns the substring of the string that starts at i and continues until j . | |
function | gmatch | Returns an iterator function that, each time it is called, returns the next captures from pattern over the string s. | |
number | len | Returns its length. | |
string, number | 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 . | |
string | format | Returns a formatted version of its variable number of arguments following the description given in its first argument. |
StartsWith
Test whether this string starts with given string
— Returns boolean.
local ret = my_string:StartsWith(other_string)
Type | Parameter | Default | Description |
---|---|---|---|
string | other_string | Required parameter | The given string |
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
— Returns boolean.
local ret = my_string:EndsWith(other_string)
Type | Parameter | Default | Description |
---|---|---|---|
string | other_string | Required parameter | The given string |
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
— Returns string.
local ret = my_string:Trim()
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
— Returns string.
local ret = my_string:FormatArgs(args...)