标签:
匹配模式(pattern)
[set]
表示 set 中所有字符的联合,找到其中任一字符即表示匹配。可以用 - 连接,如[0-9] 表示 0 到 9 这十个数字。[^set]
表示 set 的补集。字符类与特殊意义符号:
string.byte(s[, i[, j]])
例:
str = "abcdef"
print(str:byte()) --> 97,'a' 的编码值
print(str:byte(2)) --> 98,'b' 的编码值
print(str:byte(2, 4)) -> 98 99 100
string.char(...)
例:
print(string.char(97, 98, 99) --> abc
string.dump(function [, strip])
例:
local function print_hello()
print("hello world")
end
local f = string.dump(print_hello, true)
print(f)
local a = loadstring(f)
print(type(a)) --> 'function'
a() --> 'hello world'
string.find(s, pattern [, init[, plain]])
例:
str = "hello, world"
print(string.find(str, "llo")) --> 3 5
print(string.find(str, "world", -5)) --> 8 12
print(string.find(str, "world", -4)) --> nil
string.format(formating, ...)
例:
str = 'a string with "quotes" and\nnew line'
print(string.format("%q", str))
string.gmatch(s, pattern)
例:
s = "hello world from Lua"
g = string.gmatch(s, "%a+")
print(type(g)) --> 'function', g 是一个函数
print(g()) --> 'hello',调用一次则进行一次匹配
print(g()) --> 'world',返回第二次匹配的值
s = "from=world, to=Lua"
for k, v in string.gmatch(s, "(%w+)=(%w+)") do
print(k, v) --> 打印捕获到的值
end
string.gsub(s, pattern, repl[, n])
例:
s = "hello world, hello world, hello world"
print(s:gsub("world", "sam")) --> hello sam, hello sam, hello sam 3
例:
s = "hello world, hello world, hello world"
print(s:gsub("world", "sam", 1)) --> hello sam, hello world, hello world 1
print(s:gsub("world", "sam", 2)) --> hello sam, hello sam, hello world 2
例:
s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", "%1 %1", 1)) --> hello hello world, hello world, hello world 1
print(s:gsub("(%w+)%s*(%w+)", "%2 %1", 1)) --> world hello, hello world, hello world 1
例:
x = {}
x.hello = "HELLO"
x.world = "WORLD"
s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", x)) --> HELLO WORLD, HELLO WORLD, HELLO WORLD 6
例:
function x(str)
return "sam"
end
s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", x)) --> sam sam, sam sam, sam sam 6
例:
x = {}
x.hello = "HELLO"
s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", x)) --> HELLO world, HELLO world, HELLO world 6
function x(str)
return nil
end
s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", x)) --> hello world, hello world, hello world 6
string.len(s)
例:
print(string.len("hello, world")) --> 12
string.lower(s)
例:
print(string.lower("HEllo, woRLD")) --> hello, world
string.upper(s)
例:
print(string.upper("HEllo, woRLD")) --> HELLO, WORLD
string.match(s, pattern[, init])
例:
s = "hello world, hello world, hello world"
print(string.match(s, "hello")) --> hello
print(string.match(s, "wor%a+")) --> world
string.rep(s, n[, sep])
例:
print(string.rep("hello", 2)) --> hellohello
print(string.rep("hello", 2, ", ")) --> hello, hello
string.reverse(s)
例:
print(string.reverse("hello")) --> olleh
string.sub(s, i[, j])
例:
print(string.sub("hello", 2)) --> ello
print(string.sub("hello", 2, 4)) --> ell
string.pack(fmt, v1, v2, ...)
string.packsize(fmt)
string.unpack(fmt, s[, pos])
标签:
原文地址:http://www.cnblogs.com/sammei/p/lua-zi-fu-chuan-chu-li.html