Kotlin .prependIndent()

abereFejiro9953409650's avatar
Published Nov 2, 2023
Contribute to Docs

The .prependIndent() method in Kotlin is used to add indentation to a string. It is useful for formatting text, especially when it’s needed to align or indent lines within a string.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
    • Beginner Friendly.
      9 hours

Syntax

fun String.prependIndent(indent: String = " "): String
  • String: The string to add indentation to.
  • indent: String: The indentation to add to the string.

Example

This example shows how to use the .prependIndent() method to add three spaces of indentation to the beginning of the string:

fun main() {
val originalText = "'This is an indented string.'"
val indentedText = originalText.prependIndent(" ")
println(indentedText)
}

The above code will return the following output:

'This is an indented string.'

All contributors

Contribute to Docs

Learn Kotlin on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
    • Beginner Friendly.
      9 hours