.padStart()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Oct 27, 2023
Contribute to Docs

The .padStart() method pads the beginning of a string with the specified fill character. The character will be used to meet the overall length specified by the first argument given. If no character is provided the padding will be whitespace by default.

Syntax

String.padStart(length, padChar)
  • String: The string to be modified.
  • length: An integer that represents the total length of the string returned (with padding).
  • padChar: The character to be used (enclosed by '') as padding, defaults to whitespace.

Example

The example below demonstrates the use of .padStart() to pad a string.

fun main() {
val str = "codecademydocs"
val str2 = str.padStart(14)
println(str2)
val str3 = str.padStart(18, '-')
println(str3)
}

The output of this code will be:

'codecademydocs'
'----codecademydocs'

All contributors

Looking to contribute?

Learn Kotlin on Codecademy