🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
有时,我们可能需要确保字符串达到特定长度。这时候就可以使用padStart和padEnd方法了。这两个方法用于在字符串的开头和结尾填充指定的字符,直到字符串达到指定的长度。 ``` // Use the padStart method to pad "0" characters at the beginning of the string until the length is 8 const binary = '101'.padStart(8, '0'); console.log(binary); // "00000101" // Use the padEnd method to pad "*" characters at the end of the string until the length is 10 const str = "Hello".padEnd(11, " *"); console.log(str); // "Hello * * *" ```