此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

String.prototype.padEnd()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2017年4月⁩.

padEnd() 方法会将当前字符串从末尾开始填充给定的字符串(如果需要会重复填充),直到达到给定的长度。填充是从当前字符串的末尾开始的。

尝试一下

const str1 = "Breaded Mushrooms";

console.log(str1.padEnd(25, "."));
// Expected output: "Breaded Mushrooms........"

const str2 = "200";

console.log(str2.padEnd(5));
// Expected output: "200  "

语法

js
padEnd(targetLength)
padEnd(targetLength, padString)

参数

targetLength

当前 str 填充后的长度。如果该值小于或等于 str.length,则会直接返回当前 str

padString 可选

用于填充当前 str 的字符串。如果 padString 太长,无法适应 targetLength,则会被截断:对于从左到右的语言,左侧的部分将会被保留;对于从右到左的语言,右侧的部分将会被保留。默认值为“ ” (U+0020)。

返回值

在当前 str 末尾填充 padString 直到达到给定的 targetLength 所形成的 String

示例

使用 padEnd

js
"abc".padEnd(10); // "abc       "
"abc".padEnd(10, "foo"); // "abcfoofoof"
"abc".padEnd(6, "123456"); // "abc123"
"abc".padEnd(1); // "abc"

规范

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.padend

浏览器兼容性

参见