Lisp - 字串



在 Common Lisp 中,字串是向量,即字元的一維陣列。

字串字面量用雙引號括起來。字元集中支援的任何字元都可以用雙引號括起來構成字串,除了雙引號字元(")和跳脫字元(\)。但是,您可以使用反斜槓(\)對它們進行轉義來包含它們。

示例

建立一個名為 main.lisp 的新原始碼檔案,並在其中鍵入以下程式碼。

main.lisp

; print statements
(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 的原始碼檔案,並在其中鍵入以下程式碼。

main.lisp

; case-sensitive comparison for equality
(write (string= "this is test" "This is test"))
; terminate printing
(terpri)
; case-sensitive comparison for greater than
(write (string> "this is test" "This is test"))
; terminate printing
(terpri)
; case-sensitive comparison for less than
(write (string< "this is test" "This is test"))
; terminate printing
(terpri)

;case-insensitive comparision for equality
(write (string-equal "this is test" "This is test"))
; terminate printing
(terpri)
;case-insensitive comparision for greater than
(write (string-greaterp "this is test" "This is test"))
; terminate printing
(terpri)
;case-insensitive comparision for less than
(write (string-lessp "this is test" "This is test"))
; terminate printing
(terpri)

; checking non-equality
(write (string/= "this is test" "this is Test"))
; terminate printing
(terpri)
(write (string-not-equal "this is test" "This is test"))
; terminate printing
(terpri)
(write (string/= "lisp" "lisping"))
; terminate printing
(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 的原始碼檔案,並在其中鍵入以下程式碼。

main.lisp

; convert to upper case and print statement
(write-line (string-upcase "a big hello from tutorials point"))
; convert to Capital case and print statement
(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

它以字元字串作為第一個引數,以字串作為第二個引數,並返回一個子字串,其中第一個引數中的所有字元都從引數字串的開頭刪除。

3

String-right-trim

它以字元字串作為第一個引數,以字串作為第二個引數,並返回一個子字串,其中第一個引數中的所有字元都從引數字串的末尾刪除。

示例

更新名為 main.lisp 的原始碼檔案,並在其中鍵入以下程式碼。

main.lisp

; trim and print the statement
(write-line (string-trim " " "   a big hello from tutorials point   "))
; trim spaces on left and print the statement
(write-line (string-left-trim " " "   a big hello from tutorials point   "))
; trim spaces on right and print the statement
(write-line (string-right-trim " " "   a big hello from tutorials point   "))
; trim spaces on both left and right and print the statement
(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 的原始碼檔案,並在其中鍵入以下程式碼。

main.lisp

; print length of the statement
(write (length "Hello World"))
; terminate printing
(terpri)
; print substring of statement starting from index 6
(write-line (subseq "Hello World" 6))
; print char from string at index 6
(write (char "Hello World" 6))

輸出

執行程式碼時,它將返回以下結果:

11
World
#\W

字串的排序和合並

sort 函式允許對字串進行排序。它接受一個序列(向量或字串)和一個雙引數謂詞,並返回該序列的排序版本。

merge 函式接受兩個序列和一個謂詞,並返回一個序列,該序列是根據謂詞合併這兩個序列產生的。

示例

更新名為 main.lisp 的原始碼檔案,並在其中鍵入以下程式碼。

main.lisp

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

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

輸出

執行程式碼時,它將返回以下結果:

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

反轉字串

reverse 函式反轉字串。

例如,更新名為 main.lisp 的原始碼檔案,並在其中鍵入以下程式碼。

main.lisp

; print reverse of the statement
(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

連線字串

concatenate 函式連線兩個字串。這是一個通用序列函式,您必須將結果型別作為第一個引數提供。

例如,更新名為 main.lisp 的原始碼檔案,並在其中鍵入以下程式碼。

main.lisp

; concatenate and print statements
(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
廣告