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 April 2017.
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® 2025 Language Specification # sec-string.prototype.padend |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
padEnd |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.