Clojure – Strings

Clojure settings

In this guide, we will discuss Clojure Strings. A String literal is constructed in Clojure by enclosing the string text in quotations. Strings in Clojure need to be constructed using the double quotation marks such as “Hello World”.

Example

Following is an example of the usage of strings in Clojure.

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

Output

The above program produces the following output.

Hello World
This is a demo application

Basic String Operations

Clojure has a number of operations that can be performed on strings. Following are the operations.

Sr.No.String Operations & Description
1str
The concatenation of strings can be done by the simple str function.
2format
The formatting of strings can be done by the simple format function. The format function formats a string using java.lang.String.format.
3count
Returns the number of characters in the string.
4subs
Returns the substring of ‘s’ beginning at start inclusive, and ending at end (defaults to length of string), exclusive.
5compare
Returns a negative number, zero, or a positive number when ‘x’ is logically ‘less than’, ‘equal to’, or ‘greater than’ ‘y’.
6lower-case
Converts string to all lower-case.
7upper-case
Converts string to all upper-case.
8join
Returns a string of all elements in collection, as returned by (seq collection), separated by an optional separator.
9split
Splits string on a regular expression.
10split-lines
Split strings is based on the escape characters \n or \r\n.
11reverse
Reverses the characters in a string.
12replace
Replaces all instance of a match in a string with the replacement string.
13trim
Removes whitespace from both ends of the string.
14triml
Removes whitespace from the left hand side of the string.
15trimr
Removes whitespace from the right hand side of the string.

Next Topic : Click Here

This Post Has One Comment

Leave a Reply