Laying in Kotlin

I tried to break a line in Kotlin in order to achieve the correct alignment on the console output. Something like that:

accountsLoopQuery                             - "$.contactPoints.contactPoints[?(@.contactAccount.id)]"
brokerPassword                                - *****
brokerURI                                     - tcp://localhost:61616
brokerUsername                                - admin
contactPointPriorityProperties                - "contactPointPriority.properties"
customerCollection                            - "customer"
customerHistoryCollection                     - "customer_history"
defaultSystemOwner                            - "TUIGROUP"

I ended up encoding it like this: tricking Java String.format:

mutableList.forEach { cp ->
    println(String.format("%-45s - %s", cp.name, cp.value))
}

Is there any way to do this with the Kotlin libraries?

+4
source share
2 answers

You can use .padEnd(length, padChar = ' ')extension from kotlin-stdlibfor this. It takes the desired lengthand optional padChar(default is a space):

mutableList.forEach {
    println("${it.name.padEnd(45)} - ${it.value}")
}

There also padStart, which aligns the complement in a different direction.

+7
source

# format, call-site java.lang.String # format, :

mutableList.forEach { cp ->
    println("%-45s - %s".format(cp.name, cp.value))
}
+4

Source: https://habr.com/ru/post/1682239/


All Articles