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] 的内部数字编码 | |
| number, number, string | find | 在字符串中查找 pattern 的首次匹配。 | |
| string | rep | 返回一个字符串,该字符串是由 n 个字符串 s 的副本连接而成,并由字符串 sep 分隔。 | |
| string | lower | 返回此字符串的副本,其中所有大写字母均已更改为小写。 | |
| string | upper | 返回此字符串的副本,其中所有小写字母均已更改为大写。 | |
| 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 | 此字符串是否以给定的字符串开头 |
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 | 此字符串是否以给定的字符串结尾 |
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 | 修剪后的字符串 |
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 | 格式化后的字符串 |
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 | 要获取字节编码的字符串结束字符位置 |

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

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

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

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

match
在字符串中查找 pattern 的首次匹配。local ret = my_string:match(pattern, start_position)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | pattern | Required parameter | 定义应匹配内容的模式。 |
| number | start_position | Required parameter | 开始匹配的起始索引,可以为负数,以从相对于末尾的位置开始匹配。 |
Returns
| Type | Description |
|---|---|
| number | 首次匹配的捕获内容,若无捕获则为整个匹配项。未找到时返回 Nil |

reverse
返回字符串 s 反转后的字符串。local ret = my_string:reverse()
Returns
| Type | Description |
|---|---|
| string | 反转后的字符串 |

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

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

len
返回其长度。
local ret = my_string:len()
Returns
| Type | Description |
|---|---|
| number | 字符串的长度(字节) |

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

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

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