Lua教程(⼗四):字符串库详解
1. 基础字符串函数:
字符串库中有⼀些函数⾮常简单,如:
1). string.len(s) 返回字符串s的长度;
2). p(s,n) 返回字符串s重复n次的结果;
3). string.lower(s) 返回s的副本,其中所有的⼤写都被转换为了⼩写形式,其他字符不变;
4). string.upper(s) 和lower相反,将⼩写转换为⼤写;
5). string.sub(s,i,j) 提取字符串s的第i个到第j个字符。Lua中,第⼀个字符的索引值为1,最后⼀个为-1,以此类推,如:    print(string.sub("[hello world]",2,-2))      --输出hello world
6). string.format(s,...) 返回格式化后的字符串,其格式化规则等同于C语⾔中printf函数,如:
print(string.format("pi = %.4f",math.pi)) --输出pi = 3.1416
7). string.char(...) 参数为0到多个整数,并将每个整数转换为对应的字符。然后返回⼀个由这些字符连接⽽成的字符串,如:
print(string.char(97,98,99)) --输出abc
8). string.byte(s,i) 返回字符串s的第i个字符的Ascii值,如果没有第⼆个参数,缺省返回第⼀个字符的Ascii值。
复制代码代码如下:
print(string.byte("abc"))      --输出97
print(string.byte("abc",-1))  --输出99
由于字符串类型的变量都是不可变类型的变量,因此在所有和string相关的函数中,都⽆法改变参数中的字符串值,⽽是⽣成⼀个新值返回。
2. 模式匹配函数:
库函数printf详解Lua的字符串库提供了⼀组强⼤的模式匹配函数,如find、match、gsub和gmatch。
1). string.find函数:
在⽬标字符串中搜索⼀个模式,如果到,则返回匹配的起始索引和结束索引,否则返回nil。如:
复制代码代码如下:
s = "hello world"
i, j = string.find(s,"hello")
print(i, j)        --输出1  5
i, j = string.find(s,"l")
print(i, j)        --输出3  3
print(string.find(s,"lll"))  --输出nil
string.find函数还有⼀个可选参数,它是⼀个索引,⽤于告诉函数从⽬标字符串的哪个位置开始搜索。主要⽤于搜索⽬标字符串中所有匹配的⼦字符串,且每次搜索都从上⼀次到的位置开始。如:
复制代码代码如下:
local t = {}
local i = 0
while true do
i = string.find(s,"\n",i+1)
if i == nil then
break
end
t[#t + 1] = i
end
2). string.match函数:
该函数返回⽬标字符串中和模式字符串匹配的部分。如:
复制代码代码如下:
date = "Today is 2012-01-01"
d = string.match(date,"%d+\-%d+\-%d+")
print(d)  --输出2012-01-01
3). string.gsub函数:
该函数有3个参数,⽬标字符串、模式和替换字符串。基本⽤法是将⽬标字符串中所有出现模式的地⽅替换为替换字符串。如:
复制代码代码如下:
print(string.gsub("Lua is cute","cute","great"))  --输出Lua is great
该函数还有可选的第4个参数,即实际替换的次数。
复制代码代码如下:
print(string.gsub("all lii","l","x",1))  --输出axl lii
print(string.gsub("all lii","l","x",2))  --输出axx lii
函数string.gsub还有另⼀个结果,即实际替换的次数。
count = select(2, string.gsub(str," "," "))  --输出str中空格的数量
4). atch函数:
返回⼀个函数,通过这个返回的函数可以遍历到⼀个字符串中所有出现指定模式的地⽅。如:
复制代码代码如下:
words = {}
s = "hello world"
for w atch(s,"%a+") do
print(w)
words[#words + 1] = w
end
--输出结果为:
--hello
--world
3. 模式:
下⾯的列表给出了Lua⽬前⽀持的模式元字符;
这些元字符的⼤写形式表⽰它们的补集,如%A,表⽰所有⾮字母字符。
复制代码代码如下:
print(string.gsub("hello, up-down!","%S","."))  --输出hello..up.down. 4
上例中的4表⽰替换的次数。
除了上述元字符之外,Lua还提供了另外⼏个关键字符。如:( ) . % + - * ? [ ] ^ $
其中%表⽰转义字符,如%.表⽰点(.),%%表⽰百分号(%)。
⽅括号[]表⽰将不同的字符分类,即可创建出属于⾃⼰的字符分类,如[%w_]表⽰匹配字符、数字和下划线。
横线(-)表⽰连接⼀个范围,⽐如[0-9A-Z]
如果^字符在⽅括号内,如[^\n],表⽰除\n之外的所有字符,即表⽰⽅括号中的分类的补集。如果^不在⽅括号内,则表⽰以后⾯的字符开头,$和它正好相反,表⽰以前⾯的字符结束。如:^Hello%d$,匹配的字符串可能为Hello1、Hello2等。
在Lua中还提供了4种⽤来修饰模式中的重复部分,如:+(重复1次或多次)、*(重复0次或多次)、-(重复0次或多次)和?(出现0或1次)。如:
复制代码代码如下:
print(string.gsub("one, and two; and three","%a+","word")) --输出word, word word; word word
print(string.match("the number 1298 is even","%d+")) --输出1298
星号(*)和横线(-)的主要差别是,星号总是试图匹配更多的字符,⽽横线则总是试图匹配最少的字符。
4. 捕获(capture):
捕获功能可根据⼀个模式从⽬标字符串中抽出匹配于该模式的内容。在指定捕获是,应将模式中需要捕获的部分写到⼀对圆括号内。对于具有捕获的模式,函数string.match会将所有捕获到的值作为单独的结果返回。即它会将⽬标字符串切成多个捕获到的部分。如:
复制代码代码如下:
pair = "name = Anna"
key,value = string.match(pair,"(%a+)%s*=%s*(%a+)")
print(key,value)  --输出name anna
date = "Today is 2012-01-02"
y,m,d = string.match(date,"(%d+)\-(%d+)\-(%d+)")
print(y,m,d)      --输出2012    01      02
还可以对模式本⾝使⽤捕获。即%1表⽰第⼀个捕获,以此类推,%0表⽰整个匹配,如:
复制代码代码如下:
print(string.gsub("hello Lua","(.)(.)","%2%1"))  --将相邻的两个字符对调,输出为ehll ouLa
print(string.gsub("hello Lua!","%a","%0-%0"))    --输出为h-he-el-ll-lo-o L-Lu-ua-a!
5. 替换:
string.gsub函数的第三个参数不仅可以是字符串,也可以是函数或table,如果是函数,string.gsub会在
每次到匹配时调⽤该函数,调⽤时的参数就是捕获到的内容,⽽该函数的返回值则作为要替换的字符串。当⽤⼀个table来调⽤时,string.gsub 会⽤每次捕获到的内容作为key,在table中查,并将对应的value作为要替换的字符串。如果table中不包含这个key,那么string.gsub不改变这个匹配。如:
复制代码代码如下:
function expand(s)
return (string.gsub(s,"$(%w+)",_G))
end
name = "Lua"; status = "great"
print(expand("$name is $status, isn't it?"))  --输出 Lua is great, isn't it?
print(expand("$othername is $status, isn't it?"))  --输出 $othername is great, isn't it?
function expand2(s)
return (string.gsub(s,"$(%w+)",function(n) return tostring(_G[n]) end))
end
print(expand2("print = $print; a = $a")) --输出 print = function: 002B77C0; a = nil