简体   繁体   English

如何检查给定变量值是否为string类型

[英]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. 基本上我会说你必须使用(typep var 'string-type) ,但据我所知,没有像字符串这样的类型。

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: 这是一种我能用通用方式寻找的类型,因为寻找SIMPLE-ARRAY不会有任何好处:

(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) 并且使用直观的动态确定示例字符串类型的hack也没有任何好处,因为它们的长度不同(大多数时候)

(typep "rowrowrowyourboat" (type-of "string"))
> nil

So I wonder what is the canonical way to check whether a given variable is of type string? 所以我想知道检查给定变量是否为string类型的规范方法是什么?

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. 大多数类型在CL中都有一个谓词,即使字符串是一系列字符,它也存在一个函数stringp ,它完全符合你的要求。

(stringp "getlydownthestream") ; ==> T

It says in the documentation that that would be the same as writing 它在文档中说,这与写作相同

(typep "ifyouseeacrocodile" 'string) ; ==> T

Your question has several misconceptions. 你的问题有几个误解。 Usually in Lisp variables don't have a type. 通常在Lisp变量中没有类型。

Repeat: Variables in Lisp have no types . 重复: Lisp中的变量没有类型

You can ask if some Lisp value is of some type or what type it has. 您可以询问某些Lisp值是某种类型还是具有哪种类型。 Lisp objects have types attached. Lisp对象附加了类型。

Common Lisp has no type 'string'? Common Lisp没有类型'string'? Why don't you look into the documentation? 你为什么不查看文档? It is easy. 这很容易。

Common Lisp HyperSpec: http://www.lispworks.com/documentation/HyperSpec/Front/index.htm Common Lisp HyperSpec: http//www.lispworks.com/documentation/HyperSpec/Front/index.htm

Symbol Index: http://www.lispworks.com/documentation/HyperSpec/Front/X_Symbol.htm 符号索引: http//www.lispworks.com/documentation/HyperSpec/Front/X_Symbol.htm

Symbol Index for S: http://www.lispworks.com/documentation/HyperSpec/Front/X_Alph_S.htm S的符号索引: http//www.lispworks.com/documentation/HyperSpec/Front/X_Alph_S.htm

STRING: http://www.lispworks.com/documentation/HyperSpec/Body/a_string.htm#string STRING: http//www.lispworks.com/documentation/HyperSpec/Body/a_string.htm#string

System Class STRING: http://www.lispworks.com/documentation/HyperSpec/Body/t_string.htm 系统类STRING: http//www.lispworks.com/documentation/HyperSpec/Body/t_string.htm

So Common Lisp has a type STRING . 所以Common Lisp有一个STRING类型。

The Strings Dictionary also lists other string types: BASE-STRING , SIMPLE-STRING , SIMPLE-BASE-STRING . 字符串字典还列出了其他字符串类型: BASE-STRINGSIMPLE-STRINGSIMPLE-BASE-STRING

I'm using LispWorks, so the returned types look a bit different: 我正在使用LispWorks,因此返回的类型看起来有点不同:

CL-USER 20 > (type-of "foo")
SIMPLE-BASE-STRING

CL-USER 21 > (typep "foo" 'string)
T

CL-USER 22 > (stringp "foo")
T

CL-USER 23 > (subtypep 'simple-base-string 'string)
T
T

CL-USER 24 > (let ((var "foo")) (typep var 'string))
T

Secret: variables in Common Lisp can be typed, but that is another story. 秘密:Common Lisp中的变量可以输入,但这是另一个故事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查字符串是否包含给定字符串,该字符串不是另一个给定字符串的一部分? - How can I check whether a string contains a given string, that is not a part of another given string? 如何在Java中检查给定的字符串是否为布尔值? - How to check whether the given string is boolean or not in Java? 如何检查给定的字符串是否为单词 - How to check whether given string is a word 如何检查字符串中的值是否为IP地址 - How do I check whether a value in a string is an IP address Java如何检查字符串值是否是给定Class <?>的类型 - Java how to check if a string value is a type of given Class<?> 如何检查给定字符串是否多次包含另一个子字符串? - How can I check whether a given string contains another substring more than once? 如何检查输入字符串是否部分匹配 php 中给定数组中的任何单词? - How can I check whether the input string partailly matches any word in the given array in php? 我如何检查字符串是否包含用户给定单词的字母 - How can i check a string whether it contains letters of a given word by user 如何检查给定的字符串数组是否返回null? - How to check whether given string array returns null? 正则表达式-如何检查字符串是否以给定字符开始或结束? - Regexp - How to check whether a string starts OR ends with given character?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM