Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I check whether a given variable value is of type string

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?

like image 659
Sim Avatar asked Sep 01 '13 14:09

Sim


People also ask

How do you check if a variable is a 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!

How do you check if it is a string type?

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.

How do you check if a variable is a string or number?

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”.

How do you check if a variable is a type?

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.


1 Answers

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
like image 168
Sylwester Avatar answered Sep 20 '22 10:09

Sylwester