← All languages
Kotlin function of the day
Random

padEnd

Pad a string on the right to a given length.

Description

The padEnd function returns a string of at least the given length, padding the end with the specified character (default is space) if necessary. If the string is already at least the specified length, it is returned unchanged.

This is useful for formatting columns, creating fixed-width fields, or aligning text in console output.

padEnd is available on String and CharSequence types.

Arguments

NameDescriptionOptional
length The minimum desired length of the resulting string. No
padChar The character to pad with. Defaults to space. Yes

Example

println("hi".padEnd(5))          // "hi   "
println("hi".padEnd(5, '.'))     // "hi..."
println("hello".padEnd(3))       // "hello"

Reference