【问题标题】:Splitting a String into two variables? LUA将字符串拆分为两个变量?卢阿
【发布时间】:2020-12-27 08:21:20
【问题描述】:

所以在我正在编写的 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

【问题讨论】:

    标签: lua scripting


    【解决方案1】:

    试试这个:

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

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

    【讨论】:

      猜你喜欢
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 2013-12-28
      • 2020-09-12
      • 1970-01-01
      • 2019-02-05
      • 1970-01-01
      相关资源
      最近更新 更多