Skip to content

padRight

Import from @varavel/vdl-plugin-sdk/utils/strings.

function padRight(
   str, 
   length, 
   chars?): string;

Pads the right side of a string until it reaches the requested length.

Padding is added only when the string is shorter than length. The padding pattern repeats as needed and is truncated to fit exactly.

By default, spaces are used as padding. Pass chars to use a custom padding pattern. If chars is an empty string, the input is returned unchanged.

The target length is truncated with Math.trunc, and non-finite lengths are ignored by returning the original string.

Parameters

Parameter Type Description
str string String to pad.
length number Final string length to target.
chars? string Optional padding characters to repeat.

Returns

string

str padded on the right up to length characters.

Example

padRight("cat", 5, "0")
// "cat00"