Lisp-strings

提供:Dev Guides
移動先:案内検索

LISP-文字列

Common Lispの文字列はベクトル、つまり文字の1次元配列です。

文字列リテラルは二重引用符で囲みます。 文字セットでサポートされている文字は、二重引用符( ")とエスケープ文字(\)を除き、二重引用符で囲んで文字列を作成できます。 ただし、これらをバックスラッシュ(\)でエスケープすることで含めることができます。

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は、コード内の文字列を比較するための他の2つの関数セットを提供します。 1つのセットは大文字と小文字を区別し、もう1つのセットは大文字と小文字を区別しません。

次の表は、機能を提供します-

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

文字列を大文字に変換します

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

ストリングのトリミング

次の表では、文字列のトリミング機能について説明します-

Sr.No. Function & Description
1

string-trim

最初の引数として文字列を、2番目の引数として文字列を取り、最初の引数にあるすべての文字が引数文字列から削除された部分文字列を返します。

2

String-left-trim

最初の引数として文字列を、2番目の引数として文字列を取り、最初の引数にあるすべての文字が引数文字列の先頭から削除された部分文字列を返します。

3

String-right-trim

最初の引数として文字列文字を、2番目の引数として文字列を取り、最初の引数にあるすべての文字が引数文字列の末尾から削除された部分文字列を返します。

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* 関数を使用すると、文字列をソートできます。 シーケンス(ベクトルまたは文字列)と2つの引数の述語を取り、ソートされたバージョンのシーケンスを返します。
*merge* 関数は2つのシーケンスと1つの述語を取り、述語に従って2つのシーケンスをマージして生成されたシーケンスを返します。

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

文字列の連結

concatenate関数は、2つの文字列を連結します。 これは汎用シーケンス関数であり、最初の引数として結果タイプを提供する必要があります。

たとえば、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