String.prototype.trimEnd()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
String
的 trimEnd()
方法会从字符串的结尾移除空白字符,并返回一个新的字符串,而不会修改原始字符串。trimRight()
是该方法的别名。
尝试一下
const greeting = " Hello world! ";
console.log(greeting);
// Expected output: " Hello world! ";
console.log(greeting.trimEnd());
// Expected output: " Hello world!";
语法
js
trimEnd()
trimRight()
返回值
别名
示例
使用 trimEnd()
下面的示例从 str
的末尾去除空白字符,但不会去除开头的空白字符。
js
let str = " foo ";
console.log(str.length); // 8
str = str.trimEnd();
console.log(str.length); // 6
console.log(str); // ' foo'
规范
Specification |
---|
ECMAScript® 2025 Language Specification # sec-string.prototype.trimend |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
trimEnd |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- Uses a non-standard name.
- Has more compatibility info.
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.