我是靠谱客的博主 大力煎饼,这篇文章主要介绍lua 字符串分割,在Lua中用字符分割字符串,现在分享给大家,希望可以做个参考。

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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(96)

评论列表共有 0 条评论

立即
投稿
返回
顶部