Page 1 of 1

Check if is string

Posted: Fri Sep 04, 2009 6:49 am
by master121
How can i check if a variable is a string or a number etc ?

Re: Check if is string

Posted: Fri Sep 04, 2009 7:46 am
by Administrator

Code: Select all

if( type(variable) == "number" ) then
  -- it's a string
elseif( type(variable) == "string" ) then
 -- it's a number
end
However, be aware that in some cases, strings can be numbers and numbers can be strings. That is, "123", "1AC", "0.123" are all strings but can be used as numbers (use tonumber() or tostring() to convert).