我有这根绳子
Apples, Oranges, Grapes如何将最后一个逗号替换为和
对模式还没有足够的了解,但我尝试了几种方法,例如
str:gsub(",$", "and", 1)难道不是认为神奇的字符$从结束->开始读取字符串吗?
我的问题是因为我使用table.concat连接数组。
发布于 2022-02-25 16:01:24
您的桌子:
local t = {"Apples", "Oranges", "Grapes"}方法1:使用“and”连接最后一项:
local s = #t > 1
and table.concat(t, ", ", 1, #t-1).." and "..t[#t]
or table.concat(t, ", ")
print(s) --> Apples, Oranges and Grapes方法2:替换最后一个逗号:
local s = table.concat(t, ", ")
s = s:gsub("(.*),", "%1 and")
print(s) --> Apples, Oranges and Grapes发布于 2022-02-25 15:58:06
local str = "Apples, Oranges, Grapes"
print(str:gsub(",(%s+%w+)$", " and%1"))https://stackoverflow.com/questions/71268329
复制相似问题