如何进行空变量检查以避免程序错误?
- 内容介绍
- 文章标签
- 相关推荐
本文共计231个文字,预计阅读时间需要1分钟。
参考英文答案 + How to check if a value is empty in Lua? 3 ways. I am just learning Lua, and this is my first script. How do I check if a variable is empty or does not contain newline characters? Can you check if the value is zero: if emptyVar==nil then -- Some code
参见英文答案 > How to check if a value is empty in Lua?3个我只是在学习lua,这是我的第一个脚本.如何检查变量是否为空或者是否包含换行符? 您可以检查值是否为零:
if emptyVar == nil then -- Some code end
由于nil被解释为false,您还可以编写以下内容:
if not emptyVar then -- Some code end
(也就是说,除非你想检查布尔值;))
至于换行:你可以使用string.match函数:
local var1, var2 = "some string", "some\nstring with linebreaks" if string.match(var1, "\n") then print("var1 has linebreaks!") end if string.match(var2, "\n") then print("var2 has linebreaks!") end
本文共计231个文字,预计阅读时间需要1分钟。
参考英文答案 + How to check if a value is empty in Lua? 3 ways. I am just learning Lua, and this is my first script. How do I check if a variable is empty or does not contain newline characters? Can you check if the value is zero: if emptyVar==nil then -- Some code
参见英文答案 > How to check if a value is empty in Lua?3个我只是在学习lua,这是我的第一个脚本.如何检查变量是否为空或者是否包含换行符? 您可以检查值是否为零:
if emptyVar == nil then -- Some code end
由于nil被解释为false,您还可以编写以下内容:
if not emptyVar then -- Some code end
(也就是说,除非你想检查布尔值;))
至于换行:你可以使用string.match函数:
local var1, var2 = "some string", "some\nstring with linebreaks" if string.match(var1, "\n") then print("var1 has linebreaks!") end if string.match(var2, "\n") then print("var2 has linebreaks!") end

