-2

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

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

它有字母和数字!

4

1 回答 1

0

正如 Egor 建议的那样,您将编写一个函数来检查字符串是否包含任何数字或任何字母......

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

function containsDigit(str)

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

end

我敢打赌你也可以对字母做同样的事情。参考Lua 5.3 参考手册 6.4.1:字符串模式

你可以做类似的事情

local myString = "hello123"
if containsDigit(myString) and containsDigit(myString) then
  print("contains both")
end
于 2017-12-03T20:13:20.353 回答