我是靠谱客的博主 大力煎饼,最近开发中收集的这篇文章主要介绍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 字符串分割,在Lua中用字符分割字符串所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部