How to check if a variable is a string? Topic is solved
How to check if a variable is a string?
Considering that I can't find any other up-to-date threads asking this question, I'm sure I must be missing something incredibly trivial, but is there a simple way to check whether a variable is a string? I could use the expression (!IsNumber(Var) && !IsObject(Var)), but (a) I am not certain that Number, Object and String are the only possible types; and (b) it just seems weird that numbers and objects would get their own "Is Functions" but not Strings. IsAlnum(Var) would cover my requirement perfectly, but of course that throws an exception when you hand it a non-string.
Re: How to check if a variable is a string? Topic is solved
Code: Select all
if type(var)='String'
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
Re: How to check if a variable is a string?
IsNumber(1), IsNumber(1.0) and IsNumber("1") are all true, even though there are three different types of values. IsNumber tells you whether a value is numeric/can be used as a number/can be implicitly converted to a number. The hypothetical counterpart IsString would be fairly useless, because everything that isn't an object can be implicitly converted to a string, and in future (hypothetically) any object with a ToString method may also be implicitly converted to a string.
IsAlnum(var) is true if var contains only alphabetic characters and digits. It would be ambiguous/confusing to permit values other than strings, because negative numbers and floating-point numbers are numeric but not purely composed of digits.
Numbers in AutoHotkey are of type Integer or Float. Number (barely) exists as an abstract type from which Integer and Float derive (1.base.base.__class = "Number"). However, IsNumber does not perform a pure type check, unlike HasBase and is.
IsAlnum(var) is true if var contains only alphabetic characters and digits. It would be ambiguous/confusing to permit values other than strings, because negative numbers and floating-point numbers are numeric but not purely composed of digits.
Numbers in AutoHotkey are of type Integer or Float. Number (barely) exists as an abstract type from which Integer and Float derive (1.base.base.__class = "Number"). However, IsNumber does not perform a pure type check, unlike HasBase and is.
Code: Select all
Number := Class.new() ; Number is currently not predefined as a global class.
Number.prototype := 1.base.base
MsgBox 1 is Number ; 1
MsgBox 1.0 is Number ; 1
MsgBox '1' is Number ; 0
Re: How to check if a variable is a string?
You could also extend the solution above directly to strings. I don't think it's much faster than the type() comparison example, but I do this in my own code because it seems cleaner:
Code: Select all
string := class.new()
string.prototype := ''.base
s:='1', n:=1
msgbox s is string ; 1/true
msgbox n is string ; 0/false