gpt4 book ai didi

split - Lua 用分号分割字符串

转载 作者:行者123 更新时间:2023-12-01 13:39:29 31 4
gpt4 key购买 nike

如何用分号分割Lua中的字符串?

local destination_number="2233334;555555;12321315;2343242"

在这里我们可以看到多次出现分号 (;) 但我只需要在第一次出现之前从上面的字符串输出。

试过的代码:
if string.match(destination_number, ";") then
for token in string.gmatch(destination_number, "([^;]+),%s*") do
custom_destination[i] = token
i = i + 1

end
end

输出 :
2233334

我已经尝试过上面的代码,但是 Lua 脚本的新手,所以无法获得确切的语法。

最佳答案

如果你只想要第一次出现,那么这有效:

print(string.match(destination_number, "(.-);"))

该模式为:直到但不包括第一个分号的所有内容。

如果你想要所有的出现,那么这有效:
for token in string.gmatch(destination_number, "[^;]+") do
print(token)
end

关于split - Lua 用分号分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41463421/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com