📜  LISP –字符和字符串的比较运算符

📅  最后修改于: 2022-05-13 01:55:34.793000             🧑  作者: Mango

LISP –字符和字符串的比较运算符

使用运算符将字段的内容与另一个字段的内容或常量进行比较。简单来说,运算符用于比较两个或多个不同的值是否相等。

字符:

数字比较函数和运算符(例如 < 和 >)不支持字符。除此之外,使用了 Common LISP 中的两组函数。第一组区分大小写,而第二组不区分大小写或(不区分大小写)。

以下是一些可用于查看两个字符是否相等的比较函数。

Case-SensitiveCase-InsensitiveExplanation
CHAR= CHAR-EQUALChecks if the operands’ values are all equal; if they are, the condition is true.
CHAR/=CHAR-NOT-EQUALChecks if the operands’ values are all different or not; if they aren’t, the condition is true.
CHAR<CHAR-LESSPIf the character1 is smaller than character2, the condition is true; otherwise, the condition is false.
CHAR<=CHAR-NOT-GREATERPIf any of the left character’s values are less than or equal to the value of the following right character, the condition is true.
CHAR>CHAR-GREATERPIf character1 is greater than character2, the condition is true; otherwise, it is false.
CHAR>=                     CHAR-NOT-LESSP                                  If any left character’s value is more than or equal to its right character’s value, the condition is true.

示例 1:区分大小写的比较

Lisp
(write (CHAR= #\a #\A))
(terpri)
(write (CHAR> #\b #\a))
(terpri)
(write (CHAR< #\A #\a))


Lisp
(write (CHAR-EQUAL #\a #\A))
(terpri)
(write (CHAR-EQUAL #\a #\b))
(terpri)
(write (CHAR-LESSP #\a #\b #\c))
(terpri)
(write (CHAR-GREATERP #\a #\b #\c))


Lisp
(write (STRING= "gfg" "GFG"))
(terpri)
(write (STRING> "gfg" "GFG"))
(terpri)
(write (STRING< "gfg" "GFG"))


Lisp
(write (STRING-EQUAL "gfg" "GFG"))
(terpri)
(write (STRING-GREATERP "gfg" "GFG"))
(terpri)
(write (STRING-LESSP "gfg" "GFG"))


输出:运行上述代码时,输出如下。

NIL
T
T

示例 2:不区分大小写的比较

语言

(write (CHAR-EQUAL #\a #\A))
(terpri)
(write (CHAR-EQUAL #\a #\b))
(terpri)
(write (CHAR-LESSP #\a #\b #\c))
(terpri)
(write (CHAR-GREATERP #\a #\b #\c))

输出:运行上述代码时,输出如下。

T
NIL
T
NIL

字符串上的比较运算符:

在 Common Lisp 中,字符串是向量,是一维字符数组。除了双引号字符(“)和转义字符(\)之外,字符集支持的每个字符都可以包含在双引号之间形成一个字符串。和字符一样,字符串也有两组比较运算符,一组区分大小写,另一组不区分大小写。

因此,要检查两个字符串是否相等,下面是一些可以使用的比较函数。

Case-sensitiveCase-insensitiveExplanation
STRING=STRING-EQUALChecks if the operands’ values are all equal; if they are, the condition is true.
STRING/=STRING-NOT-EQUALChecks if the operands’ values are all unequal; if they are, the condition is true.
STRING<STRING-LESSPIf string1 is smaller than string2, the condition is true; otherwise, the condition is false.
STRING<=     STRING-NOT-GREATERP    If any of the left operands’ values are less than or equal to the value of the following right operand, the condition is true.
STRING>STRING-GREATERPIf string1 is greater than string2, the condition is true; otherwise, the condition is false.
STRING>=               STRING-NOT-LESSP                          If any left operand’s value is more than or equal to its right operand’s value, the condition is true.  

示例 1:区分大小写的比较。

语言

(write (STRING= "gfg" "GFG"))
(terpri)
(write (STRING> "gfg" "GFG"))
(terpri)
(write (STRING< "gfg" "GFG"))

输出:运行上述代码时,输出如下。

NIL
0
NIL

示例 2:不区分大小写的比较。

语言

(write (STRING-EQUAL "gfg" "GFG"))
(terpri)
(write (STRING-GREATERP "gfg" "GFG"))
(terpri)
(write (STRING-LESSP "gfg" "GFG"))

输出:比较运算符运行上述代码时,输出如下。

T
NIL
NIL