The String.prototype.padEnd() method in TypeScript is used to pad the current string with another string (repeated, if needed) so that the resulting string reaches a given length. The padding is applied from the end (right side) of the current string. This method is useful for creating strings of uniform length by appending characters at the end.
Syntax:
string.padEnd(targetLength: number, padString?: string): stringParameters:
- 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.
Examples of TypeScript String padEnd() method
Example 1: Basic Implementation of padEnd()
In this example, we will use the padEnd() method to add spaces at the end of a string until it reaches the specified length.
const str: string = "GeeksforGeeks";
const paddedStr: string = str.padEnd(20);
console.log(`${paddedStr} ${paddedStr.length}`);
Output:
GeeksforGeeks 20Example 2: Adding Specific Padding Characters
In this example, we will use the padEnd() method to pad the string with '0' characters until it reaches the specified length.
const str: string = "456";
const paddedStr: string = str.padEnd(6, '0');
console.log(`${paddedStr} ${paddedStr.length}`);
Output:
456000 6