Lua字符串操作

问题描述 投票:1回答:1

字符串'321 @ 322 @ 323 @ 324 @ 325'。这里每个数字的位数是3,但不限于3,可以是任何数字。字符串中有5个数字,但是这个数字可以是任何数字。任务是获取321,322,323,324,325并将其存储在表中,以便可以对它们执行任何操作。我尝试了一些字符串函数,例如c = c:gsub('%W',''),以消除那些非字母数字字符,但没有任何帮助。

function encrypter()--FUNCTION 14
    c=' '
    print('Please enter your message!')
    local message=io.read()
    lengthOfString=string.len(message)--Inbuit function to get length of a string.
    newLine()
    print('Please enter your key. Remember this key, otherwise your message wont be decrypted')
    newLine()
    key=io.read()
    key=tonumber(key)
    largeSpace()
    print("Encrypted message is")
    for s=1,lengthOfString do
        --print(encryptionFormula(string.byte(message,s),key))
        --inbuilt function for converting character of a string to it's respective ASCII value. First place gets character or variable having string whereas second place gets position of that character in the given string.
        b=encryptionFormula(string.byte(message,s),key)
        c=c..tostring(b)..'@'
        --print(c)
        b=0
    end
    print(c)
    largeSpace()
    print("Now share this message along with the key to the receiver. Don't share your key with anyone, if you don't want your message to be read.")
end
lua
1个回答
0
投票

您正在寻找的是string.gmatch()

string.gmatch()

[如果您不知道Lua模式是如何工作的,可以在local input = "123@546514@13@548@2315" local numbers = {} for number in string.gmatch(input, '%d+') do table.insert(numbers, number) end -- Output the numbers for index, number in ipairs(numbers) do print(index, number) -- This prints: -- 1 123 -- 2 546514 -- 3 13 -- 4 548 -- 5 2315 end 中阅读它们,也可以看看reference manual(第一版可免费获得Programming in Lua

© www.soinside.com 2019 - 2024. All rights reserved.