String 替换字符串中的单词而不替换重复项
我想做一些类似于String 替换字符串中的单词而不替换重复项,string,replace,lua,roblox,String,Replace,Lua,Roblox,我想做一些类似于('hello I am hello I is'):gsub('hello','hola')的事情,而不使用字符串'hola I am hola I is',而是'hello I am hola I is' 我不知道如何处理这个问题,因为它可能需要复杂的字符串操作,而在字符串操作方面,我只有4/10 我想避免的两件事是使用string.sub和for循环,但如果需要使用它们,我可以 请帮我解决我的问题一个简单的方法来替换某个出现的“hello” local text = &quo
('hello I am hello I is'):gsub('hello','hola')
的事情,而不使用字符串'hola I am hola I is'
,而是'hello I am hola I is'
我不知道如何处理这个问题,因为它可能需要复杂的字符串操作,而在字符串操作方面,我只有4/10
我想避免的两件事是使用string.sub
和for循环
,但如果需要使用它们,我可以
请帮我解决我的问题一个简单的方法来替换某个出现的
“hello”
local text = "hello I am hello I am hello"
local count = 0
local replace = 2
print((text:gsub("hello", function()
count = count + 1
if count == replace then
return "hola"
end
end)))
打印你好,我是霍拉,我是你好
或者:
print(text:gsub("hello", "hola", 2):gsub("hola", "hello", 1))
因此,将前两个
hello
替换为hola
,然后将第一个hola
替换为hello
。当然,这仅在字符串的该部分中没有其他hola
时有效。您需要提供有关可能字符串的更多信息。否则,解决方案只是将的“hello”
替换为的“hola”
,因为您不想替换的第一个解决方案前面没有空间it@Piglet如果字符串是“hello i am hello me is hello are is”,我想用hola替换第二个hello怎么办?注意,我正在与player.chatted:connect(msg)事件一起工作。