Where can I find documentation on Kotlin string formatting? I found a python equivalent of what I want here, but I can’t for the life of me find anything for Kotlin
to round this out - Kotlin supports string interpolation to handle a lot of “formatting” use cases, with the Formatter class (or other custom code) to handle specific cases. I personally rarely use Formatter, though can see use cases where that would be required.
🙏 1
c
CLOVIS
06/15/2023, 7:51 AM
And when string interpolation is not powerful enough, buildString is your friend:
Copy code
val str = buildString {
append("First line")
if (someCondition)
append("\nSecond line")
for (l in lines)
append("\n$l")
}
j
Joffrey
06/15/2023, 8:12 AM
Yes, string interpolation is nice for embedding variable parts into an otherwise fixed string,
buildString
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