字串配方 |
|
local function starts_with(str, start) return str:sub(1, #start) == start end local function ends_with(str, ending) return ending == "" or str:sub(-#ending) == ending end
請參閱 StringTrim。
str = str:gsub("^%l", string.upper)
str = str:gsub("%a", string.upper, 1)
str = str:gsub("<[^%s>]+", string.lower)
local function tchelper(first, rest) return first:upper()..rest:lower() end -- Add extra characters to the pattern if you need to. _ and ' are -- found in the middle of identifiers and English words. -- We must also put %w_' into [%w_'] to make it handle normal stuff -- and extra stuff the same. -- This also turns hex numbers into, eg. 0Xa7d4 str = str:gsub("(%a)([%w_']*)", tchelper)
範例
> str = "foo" > str = str:gsub("^%l", string.upper) > =str Foo > str = "_foo" > str = str:gsub("^%l", string.upper) > =str _foo > str = str:gsub("%a", string.upper, 1) > =str _Foo
str = str:gsub("%f[%a]%u+%f[%A]", string.lower)
在此請注意使用「邊界」正規表示式型態 %f。沒有它就難以匹配字詞邊界,包括在要匹配的字串開頭或結尾的邊界。請在字串「AAA bbb CCC dddEEE FFFhhh JJJ」上試試看。如要進一步了解,請閱讀 FrontierPattern。
請參閱 SplitJoin。
-- words and numbers for word in str:gmatch("%w+") do ... end -- identifiers in typical programming languages for id in str:gmatch("[_%a][_%w]*") do ... end -- whitespace-separated components (without handling quotes) for id in str:gmatch("%S+") do ... end
for line in str:gmatch("[^\r\n]+") do ... end
以上任何一種也可以當成函式迭代器來執行
-- call func with each word in a string str:gsub("%w+", func)
這是針對其中不含換行符號的字串設計的(亦即在 reflow 文字並將其拆分成多個段落之後)
function wrap(str, limit, indent, indent1) indent = indent or "" indent1 = indent1 or indent limit = limit or 72 local here = 1-#indent1 local function check(sp, st, word, fi) if fi - here > limit then here = st - #indent return "\n"..indent..word end end return indent1..str:gsub("(%s+)()(%S+)()", check) end
此設計建立在 wrap 的基礎上,用於快速且粗略地 reflow:段落定義為開頭為空白字元的行,或彼此之間有空白行的行
function reflow(str, limit, indent, indent1) return (str:gsub("%s*\n%s+", "\n") :gsub("%s%s+", " ") :gsub("[^\n]+", function(line) return wrap(line, limit, indent, indent1) end)) end
str:gsub(pat, "") == ""
not str:gsub(pat, ""):find"%S"
str:gsub(pat, function(s) return ok(s) and "" or "*" end) == ""
許多程式語言提供將變數格式化為字串的簡潔方法。範例
print( "%-5.5s is %5.2f" % { "pi", math.pi } ) --> pi is 3.14
請參閱 StringInterpolation 以了解如何在 Lua 中執行此作業。
注意:請另參閱 CgiUtils。
(請注意,只應該在拆分 URL 字串後再解碼;例如,這會讓你能夠正確處理查詢字串或基本部分中的引號「?」字元。)
function url_decode(str) str = str:gsub("+", " ") str = str:gsub("%%(%x%x)", function(h) return string.char(tonumber(h,16)) end) str = str:gsub("\r\n", "\n") return str end
function url_encode(str) if str then str = str:gsub("\n", "\r\n") str = str:gsub("([^%w %-%_%.%~])", function(c) return ("%%%02X"):format(string.byte(c)) end) str = str:gsub(" ", "+") end return str end
email="alex@it-rfc.de" if email:match("[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?") then print(email .. " is a valid email address") end
請參閱 CsvUtils。