This article explains how to fill a string with arbitrary characters in JavaScript .
Is there a way to fill a string with arbitrary characters?
You can use padStart() or padEnd() to fill part of the string.
padStart and padEnd to fill a string with arbitrary characters
To pad a string, use the string object’s padStart or padEnd methods.
string.padStart(length [,pad])
string.padEnd(length [,pad])
padStart pads the beginning of a string with the specified characters until the specified length is reached.
padEnd pads the end of a string with the specified characters until the specified length is reached.
let str = "JavaScript";
console.log(str.padStart(15, "*")); // "***JavaScript"
console.log(str.padEnd(15, "*")); // "JavaScript***"
In the above example, padStart() is used to add ” ” to the beginning of the string, and padEnd() is used to add ” ” to the end of the string.
summary
The following is a summary of how to fill a string with arbitrary characters.
- To pad a string, use padStart() or padEnd().
- padStart() pads the beginning of a string with the specified characters until the specified length is reached.
- padEnd() pads the end of a string with the specified characters until the specified length is reached.
To pad the string you can use padStart() or padEnd(). These methods can make a string fit a specified length by adding specified characters to the beginning or end of the string.
It was easy to understand how to use padStart and padEnd, and there was also a sample program.
Comments