在Lua中分割字符串? [英] Split string in Lua?

查看:86
本文介绍了在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆