Skip to content

Instantly share code, notes, and snippets.

@matteo-grella
Last active May 8, 2019 21:27
Show Gist options
  • Save matteo-grella/b6db6fd8aad44901fc2d8ef147ccfc75 to your computer and use it in GitHub Desktop.
Save matteo-grella/b6db6fd8aad44901fc2d8ef147ccfc75 to your computer and use it in GitHub Desktop.
Split text into substrings of N characters.
/**
* Split text into substrings of [size] characters.
*
* @param size the split size.
* @return an array of the split text.
*/
private fun String.splitToNChar(size: Int): List<String> {
val parts = ArrayList<String>()
val length = this.length
var i = 0
while (i < length) {
parts.add(this.substring(i, Math.min(length, i + size)))
i += size
}
return parts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment