This article explores different ways to split a String into chunks of the given size in Kotlin.

1. Using chunked() function

The standard solution to split a String into a list of strings is using the chunked() function, where each string does not exceed the specified size. The following code example shows invocation for this method:

Download Code

2. Using substring() function

Another option to partition a string into fixed-length chunks is using the substring() function. This would translate to a simple code below:

Download Code

3. Using Regex

Finally, we can split a string into equal size chinks around matches of the given regular expression. The following solution demonstrates this by creating a matcher and generate the start and the end index of each chunk.

Download Code

That’s all about splitting a String into chunks of the given size in Kotlin.