跳至正文
版本:bleeding-edge 🩸

string

该库提供了用于字符串操作的通用函数,例如查找和提取子字符串以及模式匹配。在 Lua 中对字符串建立索引时,第一个字符的位置是在 1(而不是像 C 语言中在 0)。索引允许为负数,并被解释为从字符串末尾向前反向索引。因此,最后一个字符在位置 -1,依此类推。


提示

你可以静态地使用此库,例如 string.method(my_str, ...),或者直接使用 my_str:method(...)

🎒 示例

(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'}"

🦠 函数

ReturnsNameDescription
booleanStartsWith测试此字符串是否以给定的字符串开头
booleanEndsWith测试此字符串是否以给定的字符串结尾
stringTrim返回一个新字符串,该字符串删除了此字符串开头和结尾的空白字符
stringFormatArgs一个更好的 string.Format,将字符串中的 {num} 替换为对应的可变参数
varargs of integerbyte返回字符 s[start_pos], s[start_pos + 1], ..., s[end_pos] 的内部数字编码
stringchar接收零个或多个整数。返回一个长度等于参数数量的字符串,其中每个字符的内部数字编码与其对应的参数相等
number, number, stringfind在字符串中查找 pattern 的首次匹配。
stringrep返回一个字符串,该字符串是由 n 个字符串 s 的副本连接而成,并由字符串 sep 分隔。
stringlower返回此字符串的副本,其中所有大写字母均已更改为小写。
stringupper返回此字符串的副本,其中所有小写字母均已更改为大写。
stringdump返回包含给定函数的二进制表示(即 *二进制块*)的字符串。
numbermatch在字符串中查找 pattern 的首次匹配。
stringreverse返回字符串 s 反转后的字符串。
stringsub返回该字符串从 i 开始并持续到 j 的子字符串。
functiongmatch返回一个迭代器函数,每次调用该函数时,都会返回在字符串 s 上通过 pattern 获取的下一个捕获项。
numberlen返回其长度。
string, numbergsub返回 s 的副本,其中所有(或前 n 个,如果给定)出现的 pattern 已被 repl 指定的替换字符串所替换。
stringformat按照第一个参数中给出的描述,返回其可变数量参数的格式化版本。
table of stringToTable将字符串拆分为单字符构成的表

StartsWith

测试此字符串是否以给定的字符串开头
local ret = my_string:StartsWith(other_string)

Parameters

TypeParameterDefaultDescription
stringother_string Required parameter 给定的字符串

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

测试此字符串是否以给定的字符串结尾
local ret = my_string:EndsWith(other_string)

Parameters

TypeParameterDefaultDescription
stringother_string Required parameter 给定的字符串

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

返回一个新字符串,该字符串删除了此字符串开头和结尾的空白字符
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

一个更好的 string.Format,将字符串中的 {num} 替换为对应的可变参数
local ret = my_string:FormatArgs(args...)

Parameters

TypeParameterDefaultDescription
varargs of anyargs... Required parameter 参数

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

返回字符 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?1
要获取字节编码的字符串起始字符位置
integerend_pos?start_pos
要获取字节编码的字符串结束字符位置

Returns

TypeDescription
varargs of integer内部数字编码

char

接收零个或多个整数。返回一个长度等于参数数量的字符串,其中每个字符的内部数字编码与其对应的参数相等
local ret = my_string:char(values...)

Parameters

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

Returns

TypeDescription
string字符串

find

在字符串中查找 pattern 的首次匹配。
local ret_1, ret_2, ret_3 = my_string:find(haystack, needle, startPos, noPatterns)

Parameters

TypeParameterDefaultDescription
stringhaystack Required parameter 要搜索的字符串。
stringneedle Required parameter 要查找的目标字符串,如果启用,则可以包含模式。
numberstartPos Required parameter 开始搜索的位置,可以为负数,负数时起始位置将相对于末尾计算。
booleannoPatterns Required parameter 禁用模式匹配。

Returns

TypeDescription
numberNo description provided
numberNo description provided
stringNo description provided

rep

返回一个字符串,该字符串是由 n 个字符串 s 的副本连接而成,并由字符串 sep 分隔。
local ret = my_string:rep(str, repetitions, separator)

Parameters

TypeParameterDefaultDescription
stringstr Required parameter 要转换的字符串。
numberrepetitions Required parameter 重复的次数,该值在内部会被四舍五入。
stringseparator Required parameter 用于分隔重复片段的字符串。请注意,它不会将此字符串添加到开头或结尾,仅添加在重复部分之间。

Returns

TypeDescription
stringNo description provided

lower

返回此字符串的副本,其中所有大写字母均已更改为小写。
local ret = my_string:lower(str)

Parameters

TypeParameterDefaultDescription
stringstr Required parameter 要转换的字符串。

Returns

TypeDescription
stringNo description provided

upper

返回此字符串的副本,其中所有小写字母均已更改为大写。
local ret = my_string:upper(str)

Parameters

TypeParameterDefaultDescription
stringstr Required parameter 要转换的字符串。

Returns

TypeDescription
stringNo description provided

dump

返回包含给定函数的二进制表示(即 *二进制块*)的字符串。
local ret = my_string:dump(func, stripDebugInfo)

Parameters

TypeParameterDefaultDescription
functionfunc Required parameter 要获取字节码的函数
booleanstripDebugInfo Required parameter 为 true 则去除调试数据,为 false 则保留

Returns

TypeDescription
stringNo description provided

match

在字符串中查找 pattern 的首次匹配。
local ret = my_string:match(string, pattern, startPosition)

Parameters

TypeParameterDefaultDescription
stringstring Required parameter 应该在其中搜索匹配项的字符串。
stringpattern Required parameter 定义应匹配内容的模式。
numberstartPosition Required parameter 开始匹配的起始索引,可以为负数,以从相对于末尾的位置开始匹配。

Returns

TypeDescription
numberNo description provided

reverse

返回字符串 s 反转后的字符串。
local ret = my_string:reverse(str)

Parameters

TypeParameterDefaultDescription
stringstr Required parameter 要反转的字符串。

Returns

TypeDescription
stringNo description provided

sub

返回该字符串从 i 开始并持续到 j 的子字符串。
local ret = my_string:sub(string, StartPos, EndPos)

Parameters

TypeParameterDefaultDescription
stringstring Required parameter 要中提取子字符串的源字符串。
numberStartPos Required parameter 将包含在子字符串中的第一个字符的位置。
numberEndPos Required parameter 将包含在子字符串中的最后一个字符的位置。可以为负数以从末尾开始倒数。

Returns

TypeDescription
stringNo description provided

gmatch

返回一个迭代器函数,每次调用该函数时,都会返回在字符串 s 上通过 pattern 获取的下一个捕获项。
local ret = my_string:gmatch(data, pattern)

Parameters

TypeParameterDefaultDescription
stringdata Required parameter 要搜索的字符串
stringpattern Required parameter 要搜索的模式

Returns

TypeDescription
functionNo description provided

len

返回其长度。
local ret = my_string:len(str)

Parameters

TypeParameterDefaultDescription
stringstr Required parameter 要获取其长度的字符串。

Returns

TypeDescription
numberNo description provided

gsub

返回 s 的副本,其中所有(或前 n 个,如果给定)出现的 pattern 已被 repl 指定的替换字符串所替换。
local ret_1, ret_2 = my_string:gsub(string, pattern, replacement, maxReplaces)

Parameters

TypeParameterDefaultDescription
stringstring Required parameter 应该被修改的字符串。
stringpattern Required parameter 定义应该匹配并最终被替换内容的模式。
stringreplacement Required parameter 如果是字符串,匹配的序列将被其替换。如果是表,匹配的序列将作为键在表中查找,若存在对应值则用作替换。如果是函数,所有匹配项都将作为参数传递给该函数,函数的返回值随后将用作替换。
numbermaxReplaces Required parameter 要进行替换的最大次数。

Returns

TypeDescription
stringNo description provided
numberNo description provided

format

按照第一个参数中给出的描述,返回其可变数量参数的格式化版本。
local ret = my_string:format(format, formatParameters)

Parameters

TypeParameterDefaultDescription
stringformat Required parameter 要格式化的字符串
numberformatParameters Required parameter 要格式化到字符串中的值。

Returns

TypeDescription
stringNo description provided

ToTable

将此字符串转换为一个表,其中每个元素都是原始字符串中的单字符子字符串。
local ret = my_string:ToTable(str)

Parameters

TypeParameterDefaultDescription
stringstr Required parameter 要转换为表的字符串

Returns

TypeDescription
table of string单字符字符串构成的表
string.ToTable Examples
Convert string to table of characters
local str = "hello"
local tbl = str:ToTable()
-- Outputs "{'h', 'e', 'l', 'l', 'o'}"