Checking an empty variable

I am just learning lua, this is my first script with it. How can I check if a variable is empty or has something like a string in it?

+4
source share
1 answer

You can check if nil value is equal:

if emptyVar == nil then -- Some code end 

Since nil is interpreted as false, you can also write the following:

 if not emptyVar then -- Some code end 

(i.e. if you do not want to check boolean values;))

Regarding linebreak: you can use the string.match function to do this:

 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 
+11
source

Source: https://habr.com/ru/post/1394760/


All Articles