gpt4 book ai didi

string - 如何检查给定变量值是否为字符串类型

转载 作者:行者123 更新时间:2023-12-02 07:08:55 26 4
gpt4 key购买 nike

本质上我会说你必须使用(typep var 'string-type),但据我所知,没有字符串这样的类型。

通过 type-of 结果确定类型

(type-of "rowrowrowyourboat")
> (SIMPLE-ARRAY CHARACTER (17))

这是一种耐寒的类型,我可以用通用的方式来寻找,因为仅仅寻找SIMPLE-ARRAY不会有任何好处:

(typep "rowrowrowyourboat" 'simple-array)
> t

(typep (make-array 1) 'simple-array)
> t

使用动态确定示例字符串类型的直观技巧也没有任何好处,因为它们的长度不同(大多数时候)

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

所以我想知道检查给定变量是否为字符串类型的规范方法是什么?

最佳答案

大多数类型在 CL 中都有谓词,即使字符串是字符序列,它也存在一个函数 stringp ,这正是您想要的。

(stringp "getlydownthestream") ; ==> T

文档中说这与写作相同

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

关于string - 如何检查给定变量值是否为字符串类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18559540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com