How to use Java methods on strings in Clojure

Overview

Since Clojure ends up running on a Java virtual environment, it is no surprise that we can utilize some of its methods.

Use Java methods on strings in Clojure

We can do this using the dot notation followed by the method we want to use. Let's see an example.

Example

(ns Project
(:gen-class))
(defn func []
(println (.toUpperCase "Hello World")))
(func)

Explanation

  • Line 3: We create a function, func .
  • Line 4: We use the toUpperCase Java method. This method converts a text in a string into capital letters.
  • Line 5: We call our func function.

Free Resources