Skip to content

Padding

Adds padding to given string.

Pads the start (left) of a string with a specified fill string a certain number of times.

  • val: string - The original string.
  • fillString?: string - The string to use for padding (default is ’ ’ | U+0020 | single whitespace ).
  • repeatCount?: number - The number of times to repeat the fill string (default is 1).
  • maxLen: number | undefined - The maximum length of the resulting string.
  • The start/left padded string.
Definition
function padStart(
val: string,
fillString?: string,
repeatCount?: number,
maxLen?: number): string
Examples
// Basic Usage
padStart('hello', '+', 3) // +++hello
// Limiting total length
padStart('hello', '-', 4, 8) // ---hello

Pads the end (right) of a string with a specified fill string a certain number of times.

  • val: string - The original string.
  • fillString?: string - The string to use for padding (default is ’ ’ | U+0020 | single whitespace ).
  • repeatCount?: number - The number of times to repeat the fill string (default is 1).
  • maxLen: number | undefined - The maximum length of the resulting string.
  • The end/right padded string.
Definition
function padEnd(
val: string,
fillString?: string,
repeatCount?: number,
maxLen?: number): string
Examples
// Basic Usage
padEnd('hello', '+', 3) // +++hello
// Limiting total length
padEnd('hello', '-', 4, 8) // ---hello

Pads a string with a specified fill string a certain number of times on both ends.

  • val: string - The original string.
  • fillString?: string - The string to use for padding (default is ’ ’ | U+0020 | single whitespace ).
  • PaddingOptions.repeatCount?: number - The number of times to repeat the fill string (default is 1).
  • PaddingOptions.maxLen?: number | undefined - The maximum length of the resulting string.
  • PaddingOptions.bias: PaddingBias - To control the distribution of padding on both sides (default is PaddingBias.END).
  • The padded string.
Definition
enum PaddingBias {
START = 0,
END = 1
}
interface PaddingOptions {
repeatCount?: number
maxLen?: number
bias?: PaddingBias
}
function padBidirectional(
val: string,
fillString?: string,
paddingOptions?: PaddingOptions): string
Examples
// Basic usage
padBidirectional('hello', '*', {repeatCount: 2}); // '**hello**'
// Limiting total length
padBidirectional('world', '-', {repeatCount: 3, maxLen: 10}); // '--world---'
// Controlling padding distribution
padBidirectional('example', '*',
{repeatCount: 2, maxLen: 10, bias: PaddingBias.START}); // '**example*'