Lua 字符串处理函数使用文档
以下是如何在 Lua 脚本中使用上面注册的字符串处理函数的文档:
len(str)
- 描述:返回字符串的长度。
- 参数:str - 要测量长度的字符串。
- 返回值:字符串的长度。
- 示例:
length = string.len("Hello World")
upper(str)
- 描述:将字符串转换为大写。
- 参数:str - 要转换的字符串。
- 返回值:转换为大写的字符串。
- 示例:
upperStr = string.upper("hello world")
lower(str)
- 描述:将字符串转换为小写。
- 参数:str - 要转换的字符串。
- 返回值:转换为小写的字符串。
- 示例:
lowerStr = string.lower("HELLO WORLD")
reverse(str)
- 描述:反转字符串中的字符顺序。
- 参数:str - 要反转的字符串。
- 返回值:反转后的字符串。
- 示例:
reversed = string.reverse("abcdef")
split(str, sep)
- 描述:根据分隔符分割字符串,并返回分割后的字符串数组。
- 参数:str - 要分割的字符串,sep - 分隔符。
- 返回值:分割后的字符串数组。
- 示例:
parts = string.split("one,two,three", ",")
join(table, sep)
- 描述:使用分隔符将字符串数组连接成一个字符串。
- 参数:table - 字符串数组,sep - 分隔符。
- 返回值:连接后的字符串。
- 示例:
str = string.join({"one", "two", "three"}, "-")
contains(str, substr)
- 描述:检查字符串是否包含指定的子字符串。
- 参数:str - 被检查的字符串,substr - 要查找的子字符串。
- 返回值:如果包含则返回 true,否则返回 false。
- 示例:
isContained = string.contains("hello world", "world")
replace(str, old, new)
- 描述:将字符串中所有的 old 子字符串替换为 new 子字符串。
- 参数:str - 要替换的字符串,old - 要被替换的子字符串,new - 替换用的子字符串。
- 返回值:替换后的字符串。
- 示例:
replaced = string.replace("hello world", "world", "Lua")
trim(str)
- 描述:去除字符串两端的空白字符。
- 参数:str - 要处理的字符串。
- 返回值:去除空白后的字符串。
- 示例:
trimmed = string.trim(" hello world ")
startswith(str, prefix)
- 描述:检查字符串是否以指定的前缀开始。
- 参数:str - 被检查的字符串,prefix - 要检查的前缀。
- 返回值:如果以指定前缀开始则返回true,否则返回false。
- 示例:
startsWithPrefix = string.startswith("hello world", "hello")
这些函数可以在 Lua 脚本中直接使用,前提是已经在 Go 程序中注册了这些函数,并且 Lua 状态机已经启动并运行。