kotlinstringbuilder

How to append a new line to StringBuilder in Kotlin


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?


Solution

  • 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")