lua 字符串處理
匹配模式(pattern)
- . 任何單個字符
- %a 任何字母
- %c 任何控制字符
- %d 任何數字
- %g 任何除空白符外的可打印字符
- %l 所有小寫字母
- %p 所有標點符號
- %s 所有空白字符
- %u 所有大寫字母
- %w 所有字母及數字
- %x 所有 16 進制數字符號
- %x (這里 x 是任何非字母/數字的字符) 表示字符 x。如 %% 表示百分號%,%. 表示點號.,%/ 表示斜杠/。
[set]表示 set 中所有字符的聯合,找到其中任一字符即表示匹配。可以用 - 連接,如[0-9] 表示 0 到 9 這十個數字。[^set]表示 set 的補集。
字符類與特殊意義符號:
- 單個字符類跟一個 '*',將匹配零個或多個該類字符,匹配盡可能長的串。
- 單個字符類跟一個 '-',將匹配零個或多個該類字符,匹配盡可能短的串。
- 單個字符類跟一個 '+',將匹配一個或多個該類字符,匹配盡可能長的串。
- 單個字符類跟一個 '?',將匹配零個或一個該類字符。
- %n,這里 n 可以從 1 到 9,匹配捕獲的第 n 個子串。
- %bxy,這里的 x 和 y 是兩個明確的字符,匹配以 x 開始 y 結束。如 %b() 匹配包括小括號在內括起來的字符串。
- %f[set],邊境模式。匹配位于 set 內某個字符之前的一個空串,且這個位置的前一個字符不屬于 set。
string.byte(s[, i[, j]])
- 返回字符 s[i], s[i+1], ..., s[j] 的數字編碼
- i 默認為 1,j 默認等于 i
例:
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])
- 以字符串形式表示一個函數,并返回。返回的字符串可用 loadstring 加載。
- 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]])
- 查找字符串 s 中第一個匹配到 pattern,返回匹配的起始和結束位置。找不到返回 nil
- init 指定從何處開始查找,默認為 1。負值表示從倒數第幾個字符開始找,直到字符串結束。
- 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, ...)
- 同 c 里的 sprintf,不支持 *, h, L, l, n, p
- 增加 %q,將一個字符串格式化為兩個雙引號括起。
例:
str = 'a string with "quotes" and\nnew line'
print(string.format("%q", str))
string.gmatch(s, pattern)
- 返回一個迭代器函數,該函數每次被調用時都會以 pattern 為模式對 s 作匹配,并返回捕獲到的值。
例:
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])
- 將字符串中所有匹配的 pattern 都替換成 repl,并返回替換后的字符串。第二個返回值返回匹配次數。
- pattern 中沒有設定捕獲則默認捕獲整個 pattern
- 默認全部進行匹配
例:
s = "hello world, hello world, hello world"
print(s:gsub("world", "sam")) --> hello sam, hello sam, hello sam 3
- 如果有 n 參數,則只替換前 n 個匹配。
例:
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
- 若 repl 是字符串,則字符串直接替換。repl 中的 %d 表示第幾個捕獲到的子串。%0 表示整個匹配。%%表示單個百分號。
例:
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
- 若 repl 是表,則每次匹配時都會用第一個捕獲值作為鍵值去查找這張表。
例:
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
- 若 repl 是函數,則每次匹配時調用該函數,捕獲值作為參數傳入該函數。
例:
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
- 表或函數的結果如果是 false 或 nil 則不操作,如果是字符串或數字,則進行替換。
例:
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)
- s 中的大寫字符轉成小寫
例:
print(string.lower("HEllo, woRLD")) --> hello, world
string.upper(s)
- s 中的小寫字符轉成大寫
例:
print(string.upper("HEllo, woRLD")) --> HELLO, WORLD
string.match(s, pattern[, init])
- 在字符串中找到第一個匹配 pattern 部分,并返回捕獲值。找不到返回 nil。
- init 指定搜索的起始位置。默認為 1,可以為負數。
例:
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])
- 用 sep 連接 n 個 s,并返回
- 默認 sep 為空,即沒有??? 分割符
例:
print(string.rep("hello", 2)) --> hellohello
print(string.rep("hello", 2, ", ")) --> hello, hello
string.reverse(s)
- 反轉字符串 s
例:
print(string.reverse("hello")) --> olleh
string.sub(s, i[, j])
- 返回 s 的子串,從 i 開始,j 結束。
- i 和 j 可以為負數。
- j 默認為 -1,即到字符串結束。
例:
print(string.sub("hello", 2)) --> ello
print(string.sub("hello", 2, 4)) --> ell

浙公網安備 33010602011771號