Split string every n characters

Viewed 11040

What would be an idiomatic way to split a string into strings of 2 characters each?

Examples:

"" -> [""]
"ab" -> ["ab"]
"abcd" -> ["ab", "cd"]

We can assume that the string has a length which is a multiple of 2.

I could use a regex like in this Java answer but I was hoping to find a better way (i.e. using one of kotlin's additional methods).

2 Answers
Related