测试如果一个变量有数字或字母或两者/无LUA

进出口试图找出如何在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

我知道这是不正确的一些论点,但这是我的目标是我的代码输出:

它有字母和数字!

回答:

正如叶戈尔认为你会写检查,如果字符串包含任何数字或字母的任何一个功能...

的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

以上是 测试如果一个变量有数字或字母或两者/无LUA 的全部内容, 来源链接: utcz.com/qa/258774.html

回到顶部