.padStart()
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
- Anonymous contributor
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