Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split String in clo​j​ure and then print

I'm new to clo​j​ure. I want to split a String and then print. If I do :

(.split "Dasher Dancer Prancer" " ")

It gives the #<String[] [Ljava.lang.String;@64e0e8ca> which is just the toString() of the array.

Then If I do :

(.length (.split "Dasher Dancer Prancer" " "))

it gives java.lang.IllegalArgumentException: No matching field found: length for class [Ljava.lang.String;

like image 698
javaguy Avatar asked Dec 11 '25 21:12

javaguy


1 Answers

Use clojure.string/split instead:

user=> (clojure.string/split "Dasher Dancer Prancer" #" ")
["Dasher" "Dancer" "Prancer"]

No need to use java interop for this.

If you need length, use count:

user=> (count (clojure.string/split "Dasher Dancer Prancer" #" "))
3

The .split you're trying to call is a simple java method call on a String, which returns an array, which then is converted by repl to String by calling toString on it. As you noticed, it's not giving you the desired result.

clojure.string/split on the other hand, returns clojure.lang.PersistentVector which has .toString method that prints the contents as expected.

like image 74
soulcheck Avatar answered Dec 14 '25 05:12

soulcheck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!