Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

strings
clojure
subs method
community creator

What is the subs method in Clojure Strings?

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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:

  1. string: The string we are working with.
  2. start: The start index position.
  3. 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)
subs method example

Explanation

From the code above:

  • Line 3: We define a function substring.
  • Line 4: We print the substring returned using the println. The subs method is used to get a substring starting from the 3rd index and ends at the 9th index whose output will be cation.
  • Line 5: We call the substring function.

RELATED TAGS

strings
clojure
subs method
community creator

CONTRIBUTOR

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring