Skip to content

padLeft

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

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

Pads the left 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 left up to length characters.

Example

padLeft("cat", 5, "0")
// "00cat"