JavaScript

StringpadBoth Method

Syntax

String.padBoth(targetLength[,paddingString])

Arguments

targetLengthnumber

The desired length of the string.

paddingStringpaddingString

The string to use as padding for adding characters to the string when the string is shorter then the target length. A space is used by default.

Returns

valuestring

The new padded string.

Description

Extension to the native string variable to support padding both sides of a string equally - a combination of the native String.padStart and String.padEnd methods. By default the separators are white-space and punctuation characters.

Example

var str = 'MA';
var str2 = str.padBoth(10);
// str2 = '    MA    '
str2 = str.padBoth(10,'-');
// str2 = '----MA----'