LISP - 字符串

Common Lisp 中的字符串是向量,即一维字符数组。

字符串文字用双引号括起来。 字符集支持的任何字符都可以用双引号括起来以形成字符串,但双引号字符 (") 和转义字符 (\) 除外。但是,您可以通过使用反斜杠 (\) 转义它们来包含这些字符。

示例

创建一个名为 main.lisp 的新源代码文件,并在其中键入以下代码。

(write-line "Hello World")
(write-line "Welcome to Tutorials Point")

;escaping the double quote character
(write-line "Welcome to \"Tutorials Point\"")

执行代码时,会返回以下结果 −

Hello World
Welcome to Tutorials Point
Welcome to "Tutorials Point"

字符串比较函数

数字比较函数和运算符,例如,< 和> 不适用于字符串。 Common LISP 提供了另外两组函数来比较代码中的字符串。 一组区分大小写,另一组不区分大小写。

下表提供了功能 −

区分大小写的函数 不区分大小写的函数 描述
string= string-equal 检查操作数的值是否全部相等,如果相等则条件成立。
string/= string-not-equal 检查操作数的值是否全部不同,如果值不相等则条件成立。
string< string-lessp 检查操作数的值是否单调递减。
string> string-greaterp 检查操作数的值是否单调递增。
string<= string-not-greaterp 检查任何左操作数的值是否大于或等于下一个右操作数的值,如果是,则条件为真。
string>= string-not-lessp 检查任何左操作数的值是否小于或等于其右操作数的值,如果是,则条件为真。

示例

创建一个名为 main.lisp 的新源代码文件,并在其中键入以下代码。

; case-sensitive comparison
(write (string= "this is test" "This is test"))
(terpri)
(write (string> "this is test" "This is test"))
(terpri)
(write (string< "this is test" "This is test"))
(terpri)

;case-insensitive comparision
(write (string-equal "this is test" "This is test"))
(terpri)
(write (string-greaterp "this is test" "This is test"))
(terpri)
(write (string-lessp "this is test" "This is test"))
(terpri)

;checking non-equal
(write (string/= "this is test" "this is Test"))
(terpri)
(write (string-not-equal "this is test" "This is test"))
(terpri)
(write (string/= "lisp" "lisping"))
(terpri)
(write (string/= "decent" "decency"))

执行代码时,会返回以下结果 −

NIL
0
NIL
T
NIL
NIL
8
NIL
4
5

转换控制函数

下表描述了转换控制函数 −

序号 函数及说明
1

string-upcase

将字符串转换为大写

2

string-downcase

将字符串转换为小写

3

string-capitalize

将字符串中的每个单词大写

示例

创建一个名为 main.lisp 的新源代码文件,并在其中键入以下代码。

(write-line (string-upcase "a big hello from tutorials point"))
(write-line (string-capitalize "a big hello from tutorials point"))

执行代码时,会返回以下结果 −

A BIG HELLO FROM TUTORIALS POINT
A Big Hello From Tutorials Point

修剪字符串

下表描述了字符串修剪函数 −

序号 函数及说明
1

string-trim

它接受一个字符串作为第一个参数,一个字符串作为第二个参数,并返回一个子字符串,其中第一个参数中的所有字符都从参数字符串中删除。

2

String-left-trim

它接受一个字符串作为第一个参数,一个字符串作为第二个参数,并返回一个子字符串,其中第一个参数中的所有字符都从参数字符串的开头删除。

< /td>
3

String-right-trim

它接受一个字符串字符作为第一个参数,一个字符串作为第二个参数,并返回一个子字符串,其中第一个参数中的所有字符都从参数字符串的末尾删除。

示例

创建一个名为 main.lisp 的新源代码文件,并在其中键入以下代码。

(write-line (string-trim " " "   a big hello from tutorials point   "))
(write-line (string-left-trim " " "   a big hello from tutorials point   "))
(write-line (string-right-trim " " "   a big hello from tutorials point   "))
(write-line (string-trim " a" "   a big hello from tutorials point   "))

执行代码时,会返回以下结果 −

a big hello from tutorials point
a big hello from tutorials point   
   a big hello from tutorials point
big hello from tutorials point

其他字符串函数

LISP 中的字符串是数组,因此也是序列。 我们将在接下来的教程中介绍这些数据类型。 所有适用于数组和序列的函数也适用于字符串。 不过,我们将使用各种示例来演示一些常用的功能。

计算长度

length 函数计算字符串的长度。

提取子字符串

subseq 函数返回从特定索引开始并继续到特定结束索引或字符串末尾的子字符串(因为字符串也是序列)。

访问字符串中的字符

char 函数允许访问字符串的各个字符。

示例

创建一个名为 main.lisp 的新源代码文件,并在其中键入以下代码。

(write (length "Hello World"))
(terpri)
(write-line (subseq "Hello World" 6))
(write (char "Hello World" 6))

执行代码时,会返回以下结果 −

11
World
#\W

字符串的排序和合并

sort 函数允许对字符串进行排序。 它接受一个序列(向量或字符串)和一个双参数谓词,并返回序列的排序版本。

merge 函数接受两个序列和一个谓词,并根据谓词返回通过合并两个序列而产生的序列。

示例

创建一个名为 main.lisp 的新源代码文件,并在其中键入以下代码。

;sorting the strings
(write (sort (vector "Amal" "Akbar" "Anthony") #'string<))
(terpri)

;merging the strings
(write (merge 'vector (vector "Rishi" "Zara" "Priyanka") 
   (vector "Anju" "Anuj" "Avni") #'string<))

执行代码时,会返回以下结果 −

#("Akbar" "Amal" "Anthony")
#("Anju" "Anuj" "Avni" "Rishi" "Zara" "Priyanka")

反转字符串

reverse 函数反转字符串。

例如,创建一个名为 main.lisp 的新源代码文件,并在其中键入以下代码。

(write-line (reverse "Are we not drawn onward, we few, drawn onward to new era"))

执行代码时,会返回以下结果 −

are wen ot drawno nward ,wef ew ,drawno nward ton ew erA

连接字符串

连接函数连接两个字符串。 这是通用序列函数,您必须提供结果类型作为第一个参数。

例如,创建一个名为 main.lisp 的新源代码文件,并在其中键入以下代码。

(write-line (concatenate 'string "Are we not drawn onward, " "we few, drawn onward to new era"))

执行代码时,会返回以下结果 −

Are we not drawn onward, we few, drawn onward to new era