user4813927
user4813927

Reputation:

Calling java.lang.String .split methode in clojure

When i call the .split methode in clojure i get: #object["[Ljava.lang.String;" 0x5aaf6982 "[Ljava.lang.String;@5aaf6982"] #object["[Ljava.lang.String;" 0x18fbbda2 "[Ljava.lang.String;@18fbbda2"]. How can i use this object in my code?

Upvotes: 0

Views: 328

Answers (2)

Diego Basch
Diego Basch

Reputation: 13079

You can use vec to convert a Java array into a vector, e.g.

(vec (.split "1,2,3,4,5" ","))
=> ["1" "2" "3" "4" "5"]

But really if you want to split a string into a Clojure collection you should be using clojure.string/split instead:

(clojure.string/split "1,2,3,4,5" #",")
=> ["1" "2" "3" "4" "5"]

Upvotes: 3

Chiron
Chiron

Reputation: 20245

Assuming that you are using .split method of Java String class, then the result is a Java array.

You can use aget function of Clojure to access the elements.

And depending on your use case, it might be better to convert the array into Clojure sequence, so you can use a vast array of functions that Clojure provides to manipulate sequences.

Upvotes: 2

Related Questions