Clojure - 字串



Clojure 中的字串字面量是用引號括起來的字串文字構建的。Clojure 中的字串需要使用雙引號構建,例如“Hello World”。

示例

以下是 Clojure 中字串用法的示例。

(ns clojure.examples.hello
   (:gen-class))
(defn hello-world []
   (println "Hello World")
   (println "This is a demo application"))
(hello-world)

輸出

以上程式產生以下輸出。

Hello World
This is a demo application

基本字串操作

Clojure 有許多可以對字串執行的操作。以下是這些操作。

序號 字串操作及描述
1 str

字串的連線可以透過簡單的 str 函式完成。

2 format

字串的格式化可以透過簡單的 format 函式完成。format 函式使用java.lang.String.format格式化字串。

3 count

返回字串中的字元數。

4 subs

返回字串 ‘s’ 從 start 開始(包含 start)到 end 結束(不包含 end,預設為字串長度)的子字串。

5 compare

當 ‘x’ 在邏輯上“小於”、“等於”或“大於”‘y’時,分別返回負數、零或正數。

6 lower-case

將字串轉換為全小寫。

7 upper-case

將字串轉換為全大寫。

8 join

返回集合中所有元素的字串,這些元素由 (seq collection) 返回,並用可選的分隔符分隔。

9 split

根據正則表示式分割字串。

10 split-lines

根據跳脫字元 \n 或 \r\n 分割字串。

11 reverse

反轉字串中的字元。

12 replace

用替換字串替換字串中所有匹配項的例項。

13 trim

刪除字串兩端的空格。

14 triml

刪除字串左側的空格。

15 trimr

刪除字串右側的空格。

廣告

© . All rights reserved.