我使用的Lua GSUB二審
local mystring = 'Thats a really nice house.'
string.gsub(mystring,"% ", "/",1)
,以取代與斜線的第一個空格字符。
但是如何只替換第二次出現的空白?
我使用的Lua GSUB二審
local mystring = 'Thats a really nice house.'
string.gsub(mystring,"% ", "/",1)
,以取代與斜線的第一個空格字符。
但是如何只替換第二次出現的空白?
您可以替換別的東西一審(假設更換不是字符串本身,您可以檢查存在),然後重新裝回:
print(mystring:gsub("% ", "\1",1):gsub("% ", "/",1):gsub("\1","% ", 1))
此打印:Thats a/really nice house.
。此外,您不需要使用%
轉義空格。
可以在string.gsub
使用函數作爲替代的價值和自己算匹配:
local mystring = "Thats a really nice house."
local cnt = 0
print(string.gsub(mystring, " ", function(m)
cnt = cnt + 1
if cnt == 2 then
return "/"
end
end))
嘗試string.gsub(mystring,"(.- .-) ", "%1/",1)
。
或'(%S +%S +)' – hjpotter92 2014-11-14 18:50:41
@ hjpotter92,那更好,是的。 – lhf 2014-11-15 02:02:36