变量 – 空变量检查

参见英文答案 > 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

相关文章

1.github代码实践源代码是lua脚本语言,下载th之后运行thmai...
此文为搬运帖,原帖地址https://www.cnblogs.com/zwywilliam/...
Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能...
localfunctiongenerate_action(params)localscale_action=cc...
2022年1月11日13:57:45 官方:https://opm.openresty.org/官...
在Lua中的table(表),就像c#中的HashMap(哈希表),key和...