繁体   English   中英

将字符串拆分为两个变量? 路亚

[英]Splitting a String into two variables? LUA

因此,在我编写的 LUA 驱动程序中,我不断收到 RS232 字符串,例如; ZAA1、ZO64、D1 等。我正在寻找一个解决方案来找到字符串的结束位置,Int 开始并将其放入两个不同的变量中? 我目前正在使用带有 string.match 方法的 while 循环。 有没有更好的办法? 下面是当前缩短的代码;

s = "ZO29"
j = 1
while j <= 64 do
    if (s == string.format("ZO%d", j)) then
        print("Within ZO message")
        inputBuffer = ""
        sendACK()
        break
        
    elseif (s == string.format("ZC%d", j)) then
        inputBuffer = ""
        sendACK() 
        break
    end
    j = j + 1
end

尝试这个:

a,b=s:match("(.-)(%d+)$")

这会将字符串末尾的数字捕获到b ,并将前面的文本捕获到a

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM