I'm new to clojure. 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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With