如何检查Lua中的值是否为空? [英] How to check if a value is empty in Lua?
问题描述
在Lua中使条件检查哪些内容为空或不为空的正确方法是什么? if x == ""
和f x ~= ""
似乎无效.
What is the proper way to make a conditional which checks of something is or is not empty in Lua? if x == ""
and f x ~= ""
does not seem to work.
推荐答案
Lua是一种基于动态类型的语言.
任何变量都可以包含以下类型之一: nil , boolean , number , string ,表,功能,线程或用户数据.
索引时,表中的任何变量(包括_G
,即全局变量所在的表)中都没有值,其值将为nil
.将表变量设置为nil
时,它实际上会取消声明"它(将条目从内存中完全删除).
声明local
变量时,如果未立即分配该变量,则将为其赋予nil
值.与表变量不同,将local
变量设置为nil
时,它不会不取消声明"它(它只是具有nil
的值).
Lua is a dynamically type-based language.
Any variable can hold one of the following types: nil, boolean, number, string, table, function, thread, or userdata.
Any variable in a table (including _G
, the table where globals reside) without a value gives a value of nil
when indexed. When you set a table variable to nil
, it essentially "undeclares" it (removing the entry from memory entirely).
When a local
variable is declared, if it is not assigned immediately it is given a value of nil
. Unlike table variable, when you set a local
variable to nil
, it does not "undeclare" it (it just has a value of nil
).
在Lua中,空字符串(""
)仍然是值",它只是大小为零的字符串.
In Lua, an empty string (""
) is still a "value" - it's simply a string of size zero.
这篇关于如何检查Lua中的值是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!