在 Lua 中拆分字符串? [英] Split string in Lua?
本文介绍了在 Lua 中拆分字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要对字符串进行简单的拆分,但似乎没有用于此的功能,而且我测试的手动方式似乎不起作用.我该怎么做?
I need to do a simple split of a string, but there doesn't seem to be a function for this, and the manual way I tested didn't seem to work. How would I do it?
推荐答案
这是我非常简单的解决方案.使用 gmatch 函数来捕获至少包含一个字符的任何字符而不是所需的分隔符的字符串.分隔符默认为 **any* 空格(Lua 中的 %s):
Here is my really simple solution. Use the gmatch function to capture strings which contain at least one character of anything other than the desired separator. The separator is **any* whitespace (%s in Lua) by default:
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
.
这篇关于在 Lua 中拆分字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文