2017-12-02 250 views
-2

進出口試圖找出如何在Lua檢查一個字符串變量中有任何字母或數字,像這樣:測試如果一個變量有數字或字母或兩者/無LUA

myAwesomeVar = "hi there crazicrafter1" 

if myAwesomeVar(has letters and numbers) then 
    print("It has letters and numbers!") 
elseif myAwesomeVar(has letters and not numbers) then 
    print("It has letters!, but no numbers...") 
elseif myAwesomeVar(has not letters and not numbers) then 
    print("It doesnt have letters or numbers...") 
elseif myAwesomeVar(has not letters and numbers) then 
    print("It doesnt have letters, but it has numbers!") 
end 

我知道這是不正確的一些論點,但這是我的目標是我的代碼輸出:

它有字母和數字!

+1

通常的方法是寫兩個函數'函數has_letters(STR)'和'功能has_numbers(STR)'每個返回布爾 –

回答

0

正如葉戈爾認爲你會寫檢查,如果字符串包含任何數字或字母的任何一個功能...

的Lua提供了方便的字符串分析字符串模式。

function containsDigit(str) 

    return string.find(str, "%d") and true or false 

end 

我敢打賭,你可以對信件做同樣的事情。請參閱Lua 5.3 Reference Manual 6.4.1: String patterns

的,你可以這樣做

local myString = "hello123" 
if containsDigit(myString) and containsDigit(myString) then 
    print("contains both") 
end 
相關問題