Search code examples
kotlinsplitchunks

Is There a Best Way to Chunk a String from the last element to first


Is there a good or best way to add some leading zeros for the string chunk that don't have an appropriate number of digits. At the code below I want to split a string where each chunk is composed of 4 digits.

To add some leading zeros to a string I use this approach:

val text = "1010111".reversed()  // 1110101 | len: 7
  .chunked(4) {  // [1110, 101 *this element only gets 3 digits]
    val sb = StringBuilder(it)
    val rem = it.length % 4  // 3

    /* [1110, 1010 -> now this element have a same digits w...] */
    if (rem != 0) sb.append("0".repeat(4 - rem)).toString()

  } // result: [1110, 1010]
  .reversed() // [1010, 1110]
  .map { it.reversed } // [0101, 0111]

val result = text.joinToString("")
println("""
   before: $text    /*  101 0111 */
   after : $result  /* 0101 0111 */
""".trimIndent())

Solution

  • This would probably be the simplest approach:

    val text  = "1010111"
    
    val result = text
        .reversed()
        .chunked(4)
        .joinToString("") { it.padStart(4, '0') }
        .reversed()
    

    reverse() does what the name implies

    chunked(4) gives us multiple list with 4 elements each. The last list might have less than 4 if the initial text is not divisible by 4

    joinToString("") let's us put the thing together, while also applying the next modifier:

    it.padStart(4, '0') adds as many '0' as necessary in order for the current chunk to be 4 characters long (basically it will only add to the final chunk

    reverse() returns our text back in the correct order.