概述
I have strings like "ABC-DEF" and I need to split them by the "-" character and assign each of the two parts to a variable. In Ruby, I would do it like:
a, b = "ABC-DEF".split('-')
Apparently, Lua doesn't have such an easy way. After some digging, I couldn't find a short and concise way to achieve what I'm after. I mention I am a complete newbie to Lua and I need to use it in a script for Redis (so it should indeed be small, a one liner if possible).
解决方案
Use pattern matching:
a, b = string.match("ABC-DEF", "(.*)%-(.*)")
Note that - is a magic character, so it must be escaped with %.
最后
以上就是大力煎饼为你收集整理的lua 字符串分割,在Lua中用字符分割字符串的全部内容,希望文章能够帮你解决lua 字符串分割,在Lua中用字符分割字符串所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复