W3cubDocs

/Kotlin

padStart

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun CharSequence.padStart(
    length: Int, 
    padChar: Char = ' '
): CharSequence

Returns a char sequence with content of this char sequence padded at the beginning to the specified length with the specified character or space.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val padWithSpace = "125".padStart(5)
println("'$padWithSpace'") // '  125'

val padWithChar = "a".padStart(5, '.')
println("'$padWithChar'") // '....a'

// string is returned as is, when its length is greater than the specified
val noPadding = "abcde".padStart(3)
println("'$noPadding'") // 'abcde'
//sampleEnd
}

Parameters

length - the desired string length.

padChar - the character to pad string with, if it has length less than the length specified. Space is used by default.

Return Returns a char sequence of length at least length consisting of this char sequence prepended with padChar as many times as are necessary to reach that length.

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun String.padStart(length: Int, padChar: Char = ' '): String

Pads the string to the specified length at the beginning with the specified character or space.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val padWithSpace = "125".padStart(5)
println("'$padWithSpace'") // '  125'

val padWithChar = "a".padStart(5, '.')
println("'$padWithChar'") // '....a'

// string is returned as is, when its length is greater than the specified
val noPadding = "abcde".padStart(3)
println("'$noPadding'") // 'abcde'
//sampleEnd
}

Parameters

length - the desired string length.

padChar - the character to pad string with, if it has length less than the length specified. Space is used by default.

Return Returns a string of length at least length consisting of this string prepended with padChar as many times as are necessary to reach that length.

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/pad-start.html