string.len(s):返回字符串s的长度。
string.rep(s,n):返回重复n次字符串s的串
string.lower(s):将s中的大写字母转换成小写
string.upper(s):将s中的小写字母转换成大写
string.sub(s,i,j):截取字符串s的从第i个字符到第j个字符之间的串。Lua中,字符串的第一个字符索引从1开始。如果是负索引,负索引从字符串的结尾向前计数:-1指向最后一个字符,-2指向倒数第二个。如果不提供第三个参数,默认为-1。
string.char:获取0个或多个整数,将每一个数字转换为字符,然后返回一个所有这些字符连接起来的字符串。
string.byte(s,i):将字符串s的第i个字符的转换成整数,第二个参数是可选的,缺省情况下i=1.
string.format:第一个参数为格式化串:由指示符和控制格式的字符组成。指示符后的控制格式的字符可以为:十进制’d’;十六进制’x’;八进制’o’;浮点数’f’;字符串’s’.
--%.4f代表小数点后面有4位小数的浮点数
print(string.format("pi = %.4f",math.pi));
d = 5; m = 11; y = 1990;
--%02d代表以固定的两位显示十进制数,不足的补0。%2d前面没有指定0,不足两位会以空白补足
print(string.format("%02d/%02d/%04d",d,m,y))
tag,title = "h1","a title"
print(string.format("<%s>%s</%s>",tag,title,tag))
输出是:pi = 3.1416
05/11/1990
<h1>a title</h1>
string.find:字符串查找,在目标串内搜索匹配指定的模式的串。函数如果找到匹配的串返回他的位置,否则返回nil.
s = "hello world"
i,j = string.find(s,"hello");
print(i,j);
--string.sub截取字符串
print(string.sub(s,i,j));
print(string.find(s,"world"));
i,j = string.find(s,"l");
print(i,j);
print(string.find(s,"lll"));
输出:
1 5
hello
7 11
3 3
nil
local s = "string.find的第三个参数是可选的:\n标示目标中搜索的起始位置。\n可用于查找目标串中所有的匹配的子串时。\n";
local t = {};
local i = 0;
while true do
i = string.find(s,"\n",i+1);
if i == nil then break end;
table.insert(t,i);
end
for k,v in pairs(t) do
print("this is table k,v "..k..":"..v);
end
输出:this is table k,v 1:45
this is table k,v 2:85
this is table k,v 3:143
string.gsub:全局字符串替换,有三个参数:目标串,模式串,替换串。第四个参数是可选的,用来限制替换的范围。第一个返回值是字符串,第二个返回值表示他进行替换操作的次数。他的基本作用是用来查找匹配模式的串,并将使用替换串将其替换掉。
s = string.gsub("Lua is cute","cute","great");
print(s);
s = string.gsub("all lii","l","x");
print(s);
s = string.gsub("Lua is great","perl","tcl");
print(s);
s = string.gsub("all lii","l","x",1)
print(s);
s = string.gsub("all lii","l","x",2);
print(s);
_,count = string.gsub("Lua is cute"," "," ");
print(count);
输出是:Lua is great
axx xii
Lua is great
axl lii
axx lii
2
string.gfind:全局字符串查找
字符串模式:
字符类:可以匹配一个特定字符集合内任何字符的模式项
.:任意字符,%a:字母,%c:控制字符,%d:数字,%l:小写字母,%p:标点字符,%s:空白符,%u:大写字母,%w:字母和数字,%x:十六进制数字,%z代表0的字符
Lua中的特殊字符:( ) . % + - * ? [ ^ $,
其中%是转义字符(\是Lua的转义字符)。
- :用于第一个字符和最后一个字符的连接,表示这两个字符之间范围内的字符集合。
^:表示其补集
+:匹配前一个字符1次或多次,总是进行最长匹配
*:匹配前一个字符0次或多次,总是进行最长匹配
-:匹配前一个字符0次或多次,总是进行最短匹配
?:匹配前一个字符0次或1次,
^:以^开头的模式只匹配目标串的开始部分
:以
:
以
结尾的模式只匹配目标串的结尾部分
%b:用来匹配对称的字符
--%A:非字母的字符(大写是小写字母的反义(补集))
print(string.gsub("hello,up-down!","%A","."));
--其中3是替换次数
输出为:hello.up.down. 3
test1 = "int x; /* x */ int y; /* y */"
print(string.gsub(test1,"/%*.*%*/","<COMMENT>"));
test2 = "int x;/* x */ int y; /* y */"
print(string.gsub(test2,"/%*.-%*/","<COMMENT>"))
输出:int x; <COMMENT> 1
int x;<COMMENT> int y; <COMMENT> 2
--%b:用来匹配对称的字符
print(string.gsub("a (enclosed (in) parentheses) line", "%b()",""));
输出:a line 1
捕获(Captures):可以使用模式串的一部分匹配目标串的一部分。将你想捕获的模式用圆括号括起来,就指定了一个capture
pair = "name = Anna";
--返回变量分别是匹配串的索引下标,然后是子模式匹配的捕获部分
index1,index2,key,value = string.find(pair,"(%a+)%s*=%s*(%a+)");
print(index1,index2,key,value)
输出:1 11 name Anna
date = "17/7/1990";
index1,index2,d,m,y = string.find(date,"(%d+)/(%d+)/(%d+)");
print(index1,index2,d,m,y);
输出:1 9 17 7 1990
%+num:用来指代前面的()
s = [[then he said : "it's all right"!]]
--其中%1对应第一个括号匹配到的内容
a,b,c,quotedPart = string.find(s, [[(["'])(.-)%1]]);
print(quotedPart)
print(c);
输出:it's all right
"
print(string.gsub("hello Lua!","(%a)","%1-%1"))
print(string.gsub("hello Lua","(.)(.)","%2%1"));
输出:h-he-el-ll-lo-o L-Lu-ua-a! 8
ehll ouLa 4
s = string.gsub("the \quote{task} is to \em{change} that","\(%a+){(.-)}","<%1>%2</%1>");
print(s);
输出:the <quote>task</quote> is to <em>change</em> that
--去除字符串首尾的空格
function trim(s)
return (string.gsub(s,"^%s*(.-)%s*$","%1"));
end
function expand(s)
s = string.gsub(s, "$(%w+)",function(n)
return _G[n];
end);
return s;
end
name = "Lua"; status = "great";
print(expand("$name is $status, isn't it?"));
输出:Lua is great, isn't it?
s = "sin(3) = $[math.sin(3)]; 2^5 = $[2^5]";
--%b用来匹配对称的字符
print((string.gsub(s, "$(%b[])",function(x)
x = "return "..string.sub(x,2,-2);
local f = loadstring(x);
return f();
end)))
输出:sin(3) = 0.14112000805987; 2^5 = 32
words = {};
for w in string.gfind("hello hi,again","(%a)") do
table.insert(words,w)
end
输出:hello
hi
again
--URL解编码
--URL解编码
function unescape(s)
s = string.gsub(s,"+", " ")
s = string.gsub(s,"%%(%x%x)",function(h)
return string.char(tonumber(h,16))
end)
return s;
end
print(unescape("a%2Bb+%3D+c"));
cgi = {};
function decode(s)
for name,value in string.gfind(s,"([^&=]+)=([^&=]+)") do
name = unescape(name);
value = unescape(value);
cgi[name] = value;
end
end
function escape(s)
s = string.gsub(s,"([&=+%c])",function(c)
return string.format("%%%02X",string.byte(c))
end)
s = string.gsub(s, " ","+");
return s;
end
function encode(t)
local s = "";
for k,v in pairs(t) do
s = s.."&"..escape(k).."="..escape(v);
end
return string.sub(s,2);
end
t = {name = "a1",query = "a+b = c",q = "yes or no"};
print(encode(t));
输出:a+b = c
q=yes+or+no&name=a1&query=a%2Bb+%3D+c
--CSV转换
function toCSV(t)
local s = "";
for _,p in pairs(t) do
s = s..","..escapeCSV(p);
end
return string.sub(s,2);
end
function escapeCSV(s)
if string.find(s, '[,"]') then
s = '"'..string.gsub(s,'"','""')..'"';
end
return s;
end
function fromCSV(s)
s = s .. ',';
local t = {};
local fieldstart = 1;
repeat
if string.find(s,'^"',fieldstart) then
local a,c;
local i = fieldstart;
repeat
a,i,c = string.find(s,'"("?)',i+1);
until c ~= '"'
if not i then error('unmatched "') end
local f = string.sub(s,fieldstart+1,i-1);
table.insert(t,(string.gsub(f,'"','"')));
fieldstart = string.find(s,',',i) + 1;
else
local nexti = string.find(s,',',fieldstart);
table.insert(t,string.sub(s,fieldstart,nexti - 1));
fieldstart = nexti + 1;
end
until fieldstart > string.len(s);
return t;
end
t = fromCSV('"hello ""hello","",""');
for i,s in ipairs(t) do print(i,s) end
输出:1 hello ""hello
2
3