💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
有时,我们可能需要确保字符串达到特定长度。这时候就可以使用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 * * *" ```