跳转到内容

string

字符串模块为lua原生自带的模块,具体使用见:lua官方手册

Xmake 中对其进行了扩展,增加了一些扩展接口:

表操作请参阅 table 模块,文件读写请参阅 io 模块

string.startswith

  • 判断字符串开头是否匹配

函数原型

API

lua
string.startswith(str: <string>, prefix: <string>)

参数说明

参数描述
str要检查的字符串
prefix要匹配的前缀字符串

用法说明

lua
local s = "hello xmake"
if s:startswith("hello") then
    print("match")
end

另请参阅 string.endswith 用于判断字符串结尾。

string.endswith

  • 判断字符串结尾是否匹配

函数原型

API

lua
string.endswith(str: <string>, suffix: <string>)

参数说明

参数描述
str要检查的字符串
suffix要匹配的后缀字符串

用法说明

lua
local s = "hello xmake"
if s:endswith("xmake") then
    print("match")
end

string.split

  • 分割字符串

函数原型

API

lua
string.split(str: <string>, separator: <string>, options: <table>)

参数说明

参数描述
str要分割的字符串
separator分隔符字符串
options分割选项表(可选)

用法说明

v2.2.7版本对这个接口做了改进,以下是对2.2.7之后版本的使用说明。

按模式匹配分割字符串,忽略空串,例如:

lua
("1\n\n2\n3"):split('\n') => 1, 2, 3
("abc123123xyz123abc"):split('123') => abc, xyz, abc
("abc123123xyz123abc"):split('[123]+') => abc, xyz, abc

按纯文本匹配分割字符串,忽略空串(省去了模式匹配,会提升稍许性能),例如:

lua
("1\n\n2\n3"):split('\n', {plain = true}) => 1, 2, 3
("abc123123xyz123abc"):split('123', {plain = true}) => abc, xyz, abc

按模式匹配分割字符串,严格匹配,不忽略空串,例如:

lua
("1\n\n2\n3"):split('\n', {strict = true}) => 1, , 2, 3
("abc123123xyz123abc"):split('123', {strict = true}) => abc, , xyz, abc
("abc123123xyz123abc"):split('[123]+', {strict = true}) => abc, xyz, abc

按纯文本匹配分割字符串,严格匹配,不忽略空串(省去了模式匹配,会提升稍许性能),例如:

lua
("1\n\n2\n3"):split('\n', {plain = true, strict = true}) => 1, , 2, 3
("abc123123xyz123abc"):split('123', {plain = true, strict = true}) => abc, , xyz, abc

限制分割块数

lua
("1\n\n2\n3"):split('\n', {limit = 2}) => 1, 2\n3
("1.2.3.4.5"):split('%.', {limit = 3}) => 1, 2, 3.4.5

string.trim

  • 去掉字符串左右空白字符

函数原型

API

lua
string.trim(str: <string>)

参数说明

参数描述
str要处理的字符串

用法说明

lua
string.trim("    hello xmake!    ")

结果为:"hello xmake!"

如只需去掉左边空白,请使用 string.ltrim;只去掉右边空白,请使用 string.rtrim

string.ltrim

  • 去掉字符串左边空白字符

函数原型

API

lua
string.ltrim(str: <string>)

参数说明

参数描述
str要处理的字符串

用法说明

lua
string.ltrim("    hello xmake!    ")

结果为:"hello xmake! "

string.rtrim

  • 去掉字符串右边空白字符

函数原型

API

lua
string.rtrim(str: <string>)

参数说明

参数描述
str要处理的字符串

用法说明

lua
string.rtrim("    hello xmake!    ")

结果为:" hello xmake!"

string.lastof

  • 查找子串最后一次出现的位置

函数原型

API

lua
string.lastof(str: <string>, pattern: <string>, plain?: <boolean>)

参数说明

参数描述
str要搜索的字符串
pattern要匹配的模式串
plain可选。是否使用纯文本匹配,默认为 false

用法说明

lua
print(("src/test/file.lua"):lastof("/", true))  -- 输出: 10
print(("abc.def.ghi"):lastof("%.", false))       -- 输出: 8

string.replace

  • 替换字符串

函数原型

API

lua
string.replace(str: <string>, old: <string>, new: <string>, opt?: <table>)

参数说明

参数描述
str要处理的字符串
old要替换的旧字符串
new替换后的新字符串
opt可选。选项表,支持 {plain = true} 进行纯文本替换

用法说明

默认使用 Lua 模式匹配替换:

lua
print(("hello world"):replace("world", "xmake"))
-- 输出: hello xmake

使用纯文本模式替换(避免特殊字符被当作模式):

lua
print(("hello (world)"):replace("(world)", "xmake", {plain = true}))
-- 输出: hello xmake

如需直接对文件内容进行替换操作,请使用 io.replaceio.gsub

string.serialize

  • 序列化对象为字符串

函数原型

API

lua
string.serialize(object: <any>, opt?: <table>)

参数说明

参数描述
object要序列化的对象
opt可选。选项表,支持 {strip = true, binary = false, indent = true}

用法说明

lua
local str = string.serialize({a = 1, b = "hello"})
print(str)

与之相反的操作是 string.deserialize,可以将序列化后的字符串还原为对象。如需将对象序列化后保存到文件,可以使用 io.save

string.deserialize

  • 反序列化字符串为对象

函数原型

API

lua
string.deserialize(str: <string>)

参数说明

参数描述
str要反序列化的字符串

用法说明

lua
local obj = ("{a = 1, b = 'hello'}"):deserialize()
print(obj.a)  -- 输出: 1
print(obj.b)  -- 输出: hello

与之相反的操作是 string.serialize。如需从文件加载序列化数据,可以使用 io.load

string.ipattern

  • 生成大小写不敏感的匹配模式

函数原型

API

lua
string.ipattern(pattern: <string>, brackets?: <boolean>)

参数说明

参数描述
patternLua 匹配模式字符串
brackets可选。是否对方括号 [] 内的字母也进行大小写转换,默认为 false

用法说明

lua
print(string.ipattern("src/.*%.c"))
-- 输出: [sS][rR][cC]/.*%.[cC]

print(("SRC/test.C"):match(string.ipattern("src/.*%.c")))
-- 输出: SRC/test.C

string.levenshtein

  • 计算两个字符串的编辑距离

函数原型

API

lua
string.levenshtein(str1: <string>, str2: <string>, opt?: <table>)

参数说明

参数描述
str1第一个字符串
str2第二个字符串
opt可选。选项表,支持 {sub = 1, ins = 1, del = 1} 分别设置替换/插入/删除的代价

用法说明

lua
print(("hello"):levenshtein("hallo"))  -- 输出: 1
print(("kitten"):levenshtein("sitting"))  -- 输出: 3