pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd

/script>

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

View in English Always switch to English

String.prototype.trimEnd()

基线 广泛可用

自 2020年1月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

StringtrimEnd() 方法会从字符串的结尾移除空白字符,并返回一个新的字符串,而不会修改原始字符串。trimRight() 是该方法的别名。

尝试一下

const greeting = "   Hello world!   ";

console.log(greeting);
// Expected output: "   Hello world!   ";

console.log(greeting.trimEnd());
// Expected output: "   Hello world!";

语法

js
trimEnd()

trimRight()

返回值

一个新的字符串,表示从 str 的末尾(右侧)去除空白字符后的结果。空白字符定义为空白符加上行终止符

如果 str 的末尾没有空白字符,仍然会返回一个新的字符串(实际上是 str 的副本)。

别名

trim() 标准化后,引擎也实现了非标准的 trimRight 方法。然而,为了与 padEnd() 保持一致,在该方法标准化时,选择了 trimEnd 作为其名称。出于 Web 兼容性的考虑,trimRight 仍然作为 trimEnd 的别名,它们引用的是完全相同的函数对象。在某些引擎中,这意味着:

js
String.prototype.trimRight.name === "trimEnd";

示例

使用 trimEnd()

下面的示例从 str 的末尾去除空白字符,但不会去除开头的空白字符。

js
let str = "   foo  ";

console.log(str.length); // 8

str = str.trimEnd();
console.log(str.length); // 6
console.log(str); // '   foo'

规范

规范
ECMAScript® 2027 Language Specification
# sec-string.prototype.trimend

浏览器兼容性

参见

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy