How to append a new line to StringBuilder in Kotlin
Asked Answered
S

1

5

In Java, Following code is one way to achieve.

StringBuilder result = new StringBuilder();
result.append(someChar);
result.append("\n");

Idiomatic way of doing it in kotlin?

Stopwatch answered 29/8, 2019 at 7:27 Comment(1)
Just remove "new"...Darbies
S
14

Kotlin 1.3 provides Extension Function to ease the task.

Usage:

val strBuilder = StringBuilder()
strBuilder.appendln("some text")

In Kotlin 1.4 appendln() is deprecated and appendLine() is introduced.

Usage:

val strBuilder = StringBuilder()
strBuilder.appendLine("some text")
Stopwatch answered 29/8, 2019 at 7:27 Comment(2)
In Kotlin 1.4 appendln() is deprecated and has been replaced by appendLine().Tramel
I don't like extension functions whose name is longer then the string they replace.Darbies

© 2022 - 2024 — McMap. All rights reserved.