What is the subs method in Clojure Strings?
Overview
A string is a sequence of characters enclosed in double quotation marks (""). One of the methods used to work with strings in Clojure is the string subs.
What is a subs method in Clojure string?
A subs method is used to get a substring of a string from a start and end index. This means that the subs method can return all or part of the strings, depending on the start and end index supplied.
Note: The index position of the first character in a string is 0.
Syntax
(subs string start end)
Syntax of subs method
Parameters
The subs method receives three parameters:
string: The string we are working with.start: The start index position.end: The position of the end index .
Return value
The subs method returns a substring of the initial string.
Example
Let's view the code for this method.
(ns clojure.examples.example(:gen-class))(defn substring [](println (subs "Education" 3 9)))(substring)
Explanation
From the code above:
- Line 3: We define a function
substring. - Line 4: We print the substring returned using the
println. Thesubsmethod is used to get a substring starting from the 3rd index and ends at the 9th index whose output will becation.
- Line 5: We call the
substringfunction.