Essentially I'd say that you'll have to use (typep var 'string-type)
, but there is no such type as string as far as I known.
Determining a type via type-of results in
(type-of "rowrowrowyourboat")
> (SIMPLE-ARRAY CHARACTER (17))
which is hardy a type I could look for in a generic way as looking for just SIMPLE-ARRAY
wouldn't do any good:
(typep "rowrowrowyourboat" 'simple-array)
> t
(typep (make-array 1) 'simple-array)
> t
And using a intuitive the hack of dynamically determining the type of an example string doesn't do any good either as they will not be of the same length (most of the time)
(typep "rowrowrowyourboat" (type-of "string"))
> nil
So I wonder what is the canonical way to check whether a given variable is of type string?
Use the typeof operator to check if a variable is a string, e.g. if (typeof variable === 'string') . If the typeof operator returns "string" , then the variable is a string. In all other cases the variable isn't a string. Copied!
To check if a variable contains a value that is a string, use the isinstance built-in function. The isinstance function takes two arguments. The first is your variable. The second is the type you want to check for.
In JavaScript, there are two ways to check if a variable is a number : isNaN() – Stands for “is Not a Number”, if variable is not a number, it return true, else return false. typeof – If variable is a number, it will returns a string named “number”.
Using the typeof Operator The typeof operator is used to check the variable type in JavaScript. It returns the type of variable. We will compare the returned value with the “boolean” string, and if it matches, we can say that the variable type is Boolean.
Most types has a predicate in CL and even if a string is a sequence of chars it exists a function, stringp, that does exactly what you want.
(stringp "getlydownthestream") ; ==> T
It says in the documentation that that would be the same as writing
(typep "ifyouseeacrocodile" 'string) ; ==> T
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With