测试变量是否具有数字或字母或两者都有/无LUA

时间:2017-12-02 23:14:12

标签: lua ti-nspire

我试图找出如何检查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 个答案:

答案 0 :(得分:0)

正如Egor建议你写一个函数来检查一个字符串是否包含任何数字或任何字母......

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