.prependIndent()
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
.
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.'
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Kotlin on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Kotlin
Learn Kotlin, the expressive, open-source programming language developed by JetBrains.Beginner Friendly9 hours