Issue
I need to split a string into a few parts that contain N letters each. For example
str = "Hello World! Kotlin is amazing!"
split_into_n(str, 3) = ["Hel", "lo ", "Wor", "ld!", " Ko", "tli", "n i", "s a", "maz", "ing", "!"]
I have tried regex, but it does not seem to work. I have tried using split methods but it doesn't work either. Any help would be appreciated :)
Solution
chunked()
extension function does exactly this:
val str = "Hello World! Kotlin is amazing!"
println(str.chunked(3))
// [Hel, lo , Wor, ld!, Ko, tli, n i, s a, maz, ing, !]
Answered By - broot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.