Michael Knudson
06/15/2023, 12:13 AMJacob
06/15/2023, 12:18 AMChris Lee
06/15/2023, 12:41 AMCLOVIS
06/15/2023, 7:51 AMval str = buildString {
append("First line")
if (someCondition)
append("\nSecond line")
for (l in lines)
append("\n$l")
}
Joffrey
06/15/2023, 8:12 AMbuildString
is the next level for conditionally concatenate parts of text, but there is still a need for actual formatting with things like String.format
for decimal numbers for instance.
These convenience extensions for dealing with format strings in Kotlin indeed rely on Java's format strings defined in the Formatter
class:
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Formatter.htmlKlitos Kyriacou
06/15/2023, 8:42 AMString.format("%.3f%n", PI)
, Kotlin offers a convenience extension function so that you can do "%.3f%n".format(PI)"
. Documented at https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/format.html but unfortunately doesn't specify the format string semantics. It should have at least mentioned it's the same as Java's.