📜  Clojure-字符串

📅  最后修改于: 2020-11-05 04:00:47             🧑  作者: Mango


字符串字面量是用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有许多可以对字符串执行的操作。以下是操作。

Sr.No. String Operations & Description
1 str

The concatenation of strings can be done by the simple str function.

2 format

The formatting of strings can be done by the simple format function. The format function formats a string using java.lang.String.format.

3 count

Returns the number of characters in the string.

4 subs

Returns the substring of ‘s’ beginning at start inclusive, and ending at end (defaults to length of string), exclusive.

5 compare

Returns a negative number, zero, or a positive number when ‘x’ is logically ‘less than’, ‘equal to’, or ‘greater than’ ‘y’.

6 lower-case

Converts string to all lower-case.

7 upper-case

Converts string to all upper-case.

8 join

Returns a string of all elements in collection, as returned by (seq collection), separated by an optional separator.

9 split

Splits string on a regular expression.

10 split-lines

Split strings is based on the escape characters \n or \r\n.

11 reverse

Reverses the characters in a string.

12 replace

Replaces all instance of a match in a string with the replacement string.

13 trim

Removes whitespace from both ends of the string.

14 triml

Removes whitespace from the left hand side of the string.

15 trimr

Removes whitespace from the right hand side of the string.