TypeScript String padEnd() method
The padEnd() method, in TypeScript, is the opposite of padStart() which can be used to insert characters at the end of a string. Unlike its counterpart padStart() it also focuses on adding padding at the string’s conclusion to maintain uniformity.
Syntax:
string.padEnd(targetLength: number, padString?: string): string
Parameters:
- targetLength: Here you have to pass the length you want the final string to be, after adding padding.
- padString(Optional): The characters used for padding. Defaults, to spaces (‘ ‘) if not specified.
Return Value:
Returns a new string of the specified targetLength with the padString applied at the end of the current string.
Example 1: The below code is a basic implementation of the padEnd() method.
Javascript
let originalStr: string = "w3wiki" ; let paddedStr: string = originalStr.padEnd(20); console.log(paddedStr, paddedStr.length); |
Output:
w3wiki 20
Example 2: The below code adds a padding as 0 to the given string till the length of 6.
Javascript
let numberString: string = "456" ; let paddedNumber: string = numberString.padEnd(6, "0" ); console.log(paddedNumber, paddedNumber.length); |
Output:
456000 6
Contact Us