--- title: String.prototype.trimRight() slug: Web/JavaScript/Reference/Global_Objects/String/trimEnd tags: - JavaScript - Method - Prototype - String translation_of: Web/JavaScript/Reference/Global_Objects/String/trimEnd original_slug: Web/JavaScript/Reference/Global_Objects/String/TrimRight --- <div>{{JSRef}}</div> <p><code><strong>trimEnd()</strong> </code>方法从一个字符串的末端移除空白字符。trimRight() 是这个方法的别名。</p> <p>{{EmbedInteractiveExample("pages/js/string-trimend.html")}}</p> <p>The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p> <h2 id="Syntax">语法</h2> <pre class="syntaxbox"><code><var>str</var>.trimEnd(); <var>str</var>.trimRight();</code></pre> <h3 id="返回值">返回值</h3> <p>一个新字符串,表示从调用字串的末(右)端除去空白。</p> <h2 id="Description">描述</h2> <p><code>trimEnd()</code> / <code>trimRight()</code>方法移除原字符串右端的连续空白符并返回,<code>trimEnd()</code> / <code>trimRight()</code>方法并不会直接修改原字符串本身。</p> <h3 id="别名">别名</h3> <p>为了与 {{jsxref("String.prototype.padEnd")}} 等函数保持一致,标准方法名称为<code>trimEnd</code>。 但是,出于Web兼容性原因,<code>trimRight</code>仍然是<code>trimEnd</code>的别名。 在某些引擎中,这意味着:</p> <pre class="brush: js"><code>String.prototype.trimRight.name === "trimEnd";</code> </pre> <h2 id="Examples">示例</h2> <h3 id="使用trimEnd">使用<code>trimEnd()</code></h3> <p>下面的例子输出了小写的字符串" foo":</p> <pre class="brush:js">var str = " foo "; alert(str.length); // 8 str = str.trimRight(); // 或写成str = str.trimEnd(); console.log(str.length); // 6 console.log(str); // ' foo' </pre> <h2 id="Specifications">Specifications</h2> <table> <thead> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> </thead> <tbody> <tr> <td><code><a href="https://github.com/tc39/proposal-string-left-right-trim/#stringprototypetrimstart--stringprototypetrimend">String.prototype.{trimStart,trimEnd}</a></code>proposal</td> <td>Stage 4</td> <td>Expected to be part of ES2019</td> </tr> </tbody> </table> <h2 id="Browser_compatibility">Browser compatibility</h2> <p>The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> <p>{{Compat("javascript.builtins.String.trimEnd")}}</p> <h2 id="See_also">相关链接</h2> <ul> <li>{{jsxref("String.prototype.trim()")}}</li> <li>{{jsxref("String.prototype.trimStart()")}}</li> </ul>