📅  最后修改于: 2020-11-03 07:13:21             🧑  作者: Mango
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"
数值比较函数和运算符(例如<和>)不适用于字符串。通用LISP提供了另外两组函数来比较代码中的字符串。一组区分大小写,另一组不区分大小写。
下表提供了功能-
Case Sensitive Functions | Case-insensitive Functions | Description |
---|---|---|
string= | string-equal | Checks if the values of the operands are all equal or not, if yes then condition becomes true. |
string/= | string-not-equal | Checks if the values of the operands are all different or not, if values are not equal then condition becomes true. |
string< | string-lessp | Checks if the values of the operands are monotonically decreasing. |
string> | string-greaterp | Checks if the values of the operands are monotonically increasing. |
string<= | string-not-greaterp | Checks if the value of any left operand is greater than or equal to the value of next right operand, if yes then condition becomes true. |
string>= | string-not-lessp | Checks if the value of any left operand is less than or equal to the value of its right operand, if yes then condition becomes true. |
创建一个名为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
下表描述了案例控制功能-
Sr.No. | Function & Description |
---|---|
1 |
string-upcase Converts the string to upper case |
2 |
string-downcase Converts the string to lower case |
3 |
string-capitalize Capitalizes each word in the string |
创建一个名为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
下表描述了字符串修剪功能-
Sr.No. | Function & Description |
---|---|
1 |
string-trim It takes a string of character(s) as first argument and a string as the second argument and returns a substring where all characters that are in the first argument are removed off the argument string. |
2 |
String-left-trim It takes a string of character(s) as first argument and a string as the second argument and returns a substring where all characters that are in the first argument are removed off the beginning of the argument string. |
3 |
String-right-trim It takes a string character(s) as first argument and a string as the second argument and returns a substring where all characters that are in the first argument are removed off the end of the argument string. |
创建一个名为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中的字符串是数组,因此也是序列。我们将在以后的教程中介绍这些数据类型。适用于数组和序列的所有函数也适用于字符串。但是,我们将使用各种示例来演示一些常用功能。
长度函数计算字符串的长度。
subseq函数返回一个子字符串(因为字符串也是一个序列),该子字符串从特定的索引开始,一直到特定的结束索引或字符串。
char函数允许访问字符串的各个字符。
例
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(write (length "Hello World"))
(terpri)
(write-line (subseq "Hello World" 6))
(write (char "Hello World" 6))
当您执行代码时,它返回以下结果-
11
World
#\W
sort函数允许对字符串进行排序。它接受一个序列(向量或字符串)和一个两个参数的谓词,并返回该序列的排序版本。
合并函数接受两个序列和一个谓词,并根据该谓词返回通过合并两个序列而产生的序列。
例
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
;sorting the strings
(write (sort (vector "Amal" "Akbar" "Anthony") #'string
当您执行代码时,它返回以下结果-
#("Akbar" "Amal" "Anthony")
#("Anju" "Anuj" "Avni" "Rishi" "Zara" "Priyanka")
反向函数反转字符串。
例如,创建一个名为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