I'm new in clojure. I'm learning about splitting string in various ways. I'm taking help from here: https://clojuredocs.org/clojure.string/split There is no example to split string at fixed number of character.
Let a string "hello everyone welcome to here". I want to split this string after every 4th char, so the output (after split) should be ["hell" "o ev" "eryo" "ne w" "elco" "me t" "o he" "re"]. Note that white space is consider a char.
can anyone tell me, how can I do this? Thanks.
To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
String split() Method: The str. split() function is used to split the given string into array of strings by separating it into substrings using a specified separator provided in the argument.
you could use re-seq
:
user> (def s "hello everyone welcome to here")
#'user/s
user> (re-seq #".{1,4}" s)
("hell" "o ev" "eryo" "ne w" "elco" "me t" "o he" "re")
or partition the string, treating it as a seq:
user> (map (partial apply str) (partition-all 4 s))
("hell" "o ev" "eryo" "ne w" "elco" "me t" "o he" "re")
(->> "hello everyone welcome to here"
(partition-all 4)
(map (partial apply str)))
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