gpt4 book ai didi

string - Lua中分割字符串?

转载 作者:行者123 更新时间:2023-12-03 04:21:59 25 4
gpt4 key购买 nike

我需要对字符串进行简单的分割,但似乎没有这个功能,而且我测试的手动方式似乎不起作用。我该怎么做?

最佳答案

这是我非常简单的解决方案。使用gmatch() 函数捕获除所需分隔符之外至少包含一个任何字符的字符串。默认情况下,分隔符是任何空格(Lua 中的%s):

function mysplit (inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end

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

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