Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在这里,我"456"用when"12345678-12345678"替换。"XXX"
"456"
"12345678-12345678"
"XXX"
s1 = string.gsub("12345678-12345678", "456", "XXX") print(s1) -- 123XXX78-123XXX78
我想替换"456",但是用索引(而不是写"456"我想使用索引),从索引替换为3索引。5"XXX"
3
5
您可以先使用索引来查找子字符串
subStr = string.sub("123456789ABCDEF", 3, 5) -- 345 newStr = string.gsub("123456789ABCDEF", subStr, "XXX") -- 12XXX6789ABCDEF
string.find 还返回开始和结束索引
strStart, strEnd = string.find("123456789ABCDEF", "345") print(strStart, strEnd) -- 3, 5