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 * * *" ```