How to check if a variable is a string? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Pscyking
Posts: 6
Joined: 25 Sep 2020, 09:49

How to check if a variable is a string?

25 Sep 2020, 17:29

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.
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: How to check if a variable is a string?  Topic is solved

25 Sep 2020, 17:35

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. »
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to check if a variable is a string?

25 Sep 2020, 18:49

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.

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
sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

Re: How to check if a variable is a string?

30 Sep 2020, 12:53

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

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: demon740, Draken, Rohwedder and 48 guests