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'}"
🦠 函数
| Returns | Name | Description | |
|---|---|---|---|
| boolean | StartsWith | 测试此字符串是否以给定的字符串开头 | |
| boolean | EndsWith | 测试此字符串是否以给定的字符串结尾 | |
| string | Trim | 返回一个新字符串,该字符串删除了此字符串开头和结尾的空白字符 | |
| string | FormatArgs | 一个更好的 string.Format,将字符串中的 {num} 替换为对应的可变参数 | |
| varargs of integer | byte | 返回字符 s[start_pos], s[start_pos + 1], ..., s[end_pos] 的内部数字编码 | |
| string | char | 接收零个或多个整数。返回一个长度等于参数数量的字符串,其中每个字符的内部数字编码与其对应的参数相等 | |
| number, number, string | find | 在字符串中查找 pattern 的首次匹配。 | |
| string | rep | 返回一个字符串,该字符串是由 n 个字符串 s 的副本连接而成,并由字符串 sep 分隔。 | |
| string | lower | 返回此字符串的副本,其中所有大写字母均已更改为小写。 | |
| string | upper | 返回此字符串的副本,其中所有小写字母均已更改为大写。 | |
| string | dump | 返回包含给定函数的二进制表示(即 *二进制块*)的字符串。 | |
| number | match | 在字符串中查找 pattern 的首次匹配。 | |
| string | reverse | 返回字符串 s 反转后的字符串。 | |
| string | sub | 返回该字符串从 i 开始并持续到 j 的子字符串。 | |
| function | gmatch | 返回一个迭代器函数,每次调用该函数时,都会返回在字符串 s 上通过 pattern 获取的下一个捕获项。 | |
| number | len | 返回其长度。 | |
| string, number | gsub | 返回 s 的副本,其中所有(或前 n 个,如果给定)出现的 pattern 已被 repl 指定的替换字符串所替换。 | |
| string | format | 按照第一个参数中给出的描述,返回其可变数量参数的格式化版本。 | |
| table of string | ToTable | 将字符串拆分为单字符构成的表 |

StartsWith
测试此字符串是否以给定的字符串开头
local ret = my_string:StartsWith(other_string)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | other_string | Required parameter | 给定的字符串 |
Returns
| Type | Description |
|---|---|
| boolean | No 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
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | other_string | Required parameter | 给定的字符串 |
Returns
| Type | Description |
|---|---|
| boolean | No 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
| Type | Description |
|---|---|
| string | No 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...)
Returns
| Type | Description |
|---|---|
| string | No 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
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | start_pos? | 1 | 要获取字节编码的字符串起始字符位置 |
| integer | end_pos? | start_pos | 要获取字节编码的字符串结束字符位置 |

char
接收零个或多个整数。返回一个长度等于参数数量的字符串,其中每个字符的内部数字编码与其对应的参数相等
local ret = my_string:char(values...)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| varargs of integer | values... | Required parameter | No description provided |
Returns
| Type | Description |
|---|---|
| string | 字符串 |

find
在字符串中查找 pattern 的首次匹配。local ret_1, ret_2, ret_3 = my_string:find(haystack, needle, startPos, noPatterns)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | haystack | Required parameter | 要搜索的字符串。 |
| string | needle | Required parameter | 要查找的目标字符串,如果启用,则可以包含模式。 |
| number | startPos | Required parameter | 开始搜索的位置,可以为负数,负数时起始位置将相对于末尾计算。 |
| boolean | noPatterns | Required parameter | 禁用模式匹配。 |
Returns
| Type | Description |
|---|---|
| number | No description provided |
| number | No description provided |
| string | No description provided |

rep
返回一个字符串,该字符串是由n个字符串s的副本连接而成,并由字符串sep分隔。
local ret = my_string:rep(str, repetitions, separator)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | str | Required parameter | 要转换的字符串。 |
| number | repetitions | Required parameter | 重复的次数,该值在内部会被四舍五入。 |
| string | separator | Required parameter | 用于分隔重复片段的字符串。请注意,它不会将此字符串添加到开头或结尾,仅添加在重复部分之间。 |
Returns
| Type | Description |
|---|---|
| string | No description provided |

lower
返回此字符串的副本,其中所有大写字母均已更改为小写。
local ret = my_string:lower(str)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | str | Required parameter | 要转换的字符串。 |
Returns
| Type | Description |
|---|---|
| string | No description provided |

upper
返回此字符串的副本,其中所有小写字母均已更改为大写。
local ret = my_string:upper(str)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | str | Required parameter | 要转换的字符串。 |
Returns
| Type | Description |
|---|---|
| string | No description provided |

dump
返回包含给定函数的二进制表示(即 *二进制块*)的字符串。
local ret = my_string:dump(func, stripDebugInfo)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| function | func | Required parameter | 要获取字节码的函数 |
| boolean | stripDebugInfo | Required parameter | 为 true 则去除调试数据,为 false 则保留 |
Returns
| Type | Description |
|---|---|
| string | No description provided |

match
在字符串中查找 pattern 的首次匹配。local ret = my_string:match(string, pattern, startPosition)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | string | Required parameter | 应该在其中搜索匹配项的字符串。 |
| string | pattern | Required parameter | 定义应匹配内容的模式。 |
| number | startPosition | Required parameter | 开始匹配的起始索引,可以为负数,以从相对于末尾的位置开始匹配。 |
Returns
| Type | Description |
|---|---|
| number | No description provided |

reverse
返回字符串 s 反转后的字符串。local ret = my_string:reverse(str)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | str | Required parameter | 要反转的字符串。 |
Returns
| Type | Description |
|---|---|
| string | No description provided |

sub
返回该字符串从i开始并持续到j的子字符串。
local ret = my_string:sub(string, StartPos, EndPos)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | string | Required parameter | 要中提取子字符串的源字符串。 |
| number | StartPos | Required parameter | 将包含在子字符串中的第一个字符的位置。 |
| number | EndPos | Required parameter | 将包含在子字符串中的最后一个字符的位置。可以为负数以从末尾开始倒数。 |
Returns
| Type | Description |
|---|---|
| string | No description provided |

gmatch
返回一个迭代器函数,每次调用该函数时,都会返回在字符串 s 上通过 pattern 获取的下一个捕获项。local ret = my_string:gmatch(data, pattern)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | data | Required parameter | 要搜索的字符串 |
| string | pattern | Required parameter | 要搜索的模式 |
Returns
| Type | Description |
|---|---|
| function | No description provided |

len
返回其长度。
local ret = my_string:len(str)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | str | Required parameter | 要获取其长度的字符串。 |
Returns
| Type | Description |
|---|---|
| number | No description provided |

gsub
返回 s 的副本,其中所有(或前n个,如果给定)出现的pattern已被repl指定的替换字符串所替换。
local ret_1, ret_2 = my_string:gsub(string, pattern, replacement, maxReplaces)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | string | Required parameter | 应该被修改的字符串。 |
| string | pattern | Required parameter | 定义应该匹配并最终被替换内容的模式。 |
| string | replacement | Required parameter | 如果是字符串,匹配的序列将被其替换。如果是表,匹配的序列将作为键在表中查找,若存在对应值则用作替换。如果是函数,所有匹配项都将作为参数传递给该函数,函数的返回值随后将用作替换。 |
| number | maxReplaces | Required parameter | 要进行替换的最大次数。 |

format
按照第一个参数中给出的描述,返回其可变数量参数的格式化版本。
local ret = my_string:format(format, formatParameters)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | format | Required parameter | 要格式化的字符串 |
| number | formatParameters | Required parameter | 要格式化到字符串中的值。 |
Returns
| Type | Description |
|---|---|
| string | No description provided |

ToTable
将此字符串转换为一个表,其中每个元素都是原始字符串中的单字符子字符串。
local ret = my_string:ToTable(str)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | str | Required parameter | 要转换为表的字符串 |
string.ToTable Examples
Convert string to table of characters
local str = "hello"
local tbl = str:ToTable()
-- Outputs "{'h', 'e', 'l', 'l', 'o'}"