Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 替换字符串中的单词而不替换重复项_String_Replace_Lua_Roblox - Fatal编程技术网

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)事件一起工作。