blob: a3422659aa4a6c62eb39adf878b5c566a8265f00 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
---
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" name="Syntax">语法</h2>
<pre class="syntaxbox"><code><var>str</var>.trimEnd();
<var>str</var>.trimRight();</code></pre>
<h3 id="返回值">返回值</h3>
<p>一个新字符串,表示从调用字串的末(右)端除去空白。</p>
<h2 id="Description" name="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" name="Examples">示例</h2>
<h3 id="使用trimEnd">使用<code>trimEnd()</code></h3>
<p>下面的例子输出了小写的字符串" foo":</p>
<pre class="brush:js;highlight:[5]">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" name="See_also">相关链接</h2>
<ul>
<li>{{jsxref("String.prototype.trim()")}}</li>
<li>{{jsxref("String.prototype.trimStart()")}}</li>
</ul>
|