在Lua中分割字符串,保存在2个变量中 [英] Split string in Lua, save in 2 variables

查看:357
本文介绍了在Lua中分割字符串,保存在2个变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,即= s = "Pedro Martinez, Defense"

I have one string, that is = s = "Pedro Martinez, Defense"

我想在逗号之前和之后分割字符串,将这些剪切存储在2个变量中,例如:

I want to split the string before the comma and after the comma, store those cuts on 2 variables, for example:

我认为我需要使用string.gmatch函数或string.sub

I think that I need to use the string.gmatch function or string.sub

我该怎么做?

推荐答案

The accepted answer at How to convert GPS coordinates to decimal in Lua? shows how to use string.match and patterns. Applying the same techniques as mentioned there, you could use

local name, expertise = string.match(s, "(.*),%s*(%a*)")

这将导致名称为"Pedro Martinez"和专业知识为"Defense".

which would lead to name being "Pedro Martinez" and expertise being "Defense".

这篇关于在Lua中分割字符串,保存在2个变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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