ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
有时,我们可能需要确保字符串达到特定长度。这时候就可以使用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 * * *" ```