Sling Academy
Home/Kotlin/Working with Multiline Strings in Kotlin

Working with Multiline Strings in Kotlin

Last updated: November 29, 2024

When working with strings in Kotlin, sometimes you might encounter scenarios where a multiline string would serve better for your use case. Kotlin addresses this with what's called raw strings. These are wrapped in triple quotes (""") and they retain formatting such as newlines and indentations without the need for escape characters.

Declaration of Multiline Strings

Let's start by showing how to declare a basic multiline string in Kotlin:


fun main() {
    val multilineString = """
        This is a 
        multiline string
        in Kotlin.
    """
    println(multilineString)
}

The output of this snippet would be:


This is a 
multiline string
in Kotlin.

Note how the newlines and spaces are preserved. The raw string ignores leading spaces on each line to maintain indentation from the body of the code.

Trimming Indentations

If you want to trim the preceding blank spaces generated by indentation, you can make use of the trimMargin() or trimIndent() functions.

Using trimMargin()

The trimMargin() function removes whitespace before a delimiter and the delimiter itself. By default, the delimiter is |.


fun main() {
    val text = """
        |This is line 1
        |This is line 2
        |This is line 3
    """.trimMargin()
    println(text)
}

The output will be:


This is line 1
This is line 2
This is line 3

The trimMargin() function is particularly handy when you want to remove leading whitespace but maintain alignment in the code.

Using trimIndent()

The trimIndent() function is used to remove the leading whitespace from each line so that the minimum common sequence is determined and removed.


fun main() {
    val text = """
        Quick brown fox
        jumps over the lazy dog.
        Keele code example.
    """.trimIndent()
    println(text)
}

The output will be:


Quick brown fox
jumps over the lazy dog.
Keele code example.

Combining Multiline Strings

Sometimes, you might want to combine multiple multiline strings together. Simply use the string concatenation operator +:


fun main() {
    val part1 = """
        Hello,
        this is part 1.
    """
    val part2 = """
        And this is part 2.
    """
    val combined = part1 + part2
    println(combined)
}

The output will be:


Hello,
this is part 1.

And this is part 2.

Notice that concatenation maintains the formatting of each string. Multiline strings are flexible and make handling complex string formats more readable and manageable. By using the techniques covered in this article, you can effectively utilize multiline strings in Kotlin for your projects.

Next Article: Understanding Numbers in Kotlin

Previous Article: Advanced String Manipulation Techniques in Kotlin

Series: Primitive data types in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin