393 lines
37 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<p><strong><code>String</code></strong> 全局对象是一个用于字符串或一个字符序列的构造函数。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<p>字符串字面量采取以下形式:</p>
<pre><code class="language-javascript">'string text'
"string text"
"中文/汉语"
"español"
"English "
"हिन्दी"
"العربية"
"português"
"বাংলা"
"русский"
"日本語"
"ਪੰਜਾਬੀ"
"한국어"</code></pre>
<p>你也能使用 <code>String</code> 函数将其他值生成或转换成字符串:</p>
<pre><code class="language-javascript">String(thing)
new String(thing)</code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
<dt><code>thing</code></dt>
<dd>任何可以被转换成字符串的值。</dd>
</dl>
<h3 id="Parameters" name="Parameters">模板字面量</h3>
<p>从 ECMAScript 2015 开始,字符串字面量也可以称为<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">模板字面量</a></p>
<pre>`hello world` `hello! world!` `hello ${who}` escape `&lt;a&gt;${who}&lt;/a&gt;`</code></pre>
<h3 id="Parameters" name="Parameters">转义字符</h3>
<dl>
</dl>
<p>除了普通的可打印字符以外,一些特殊有特殊功能的字符可以通过转义字符的形式放入字符串中:</p>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Code</th>
<th scope="col">Output</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>\0</code></td>
<td>空字符</td>
</tr>
<tr>
<td><code>\'</code></td>
<td>单引号</td>
</tr>
<tr>
<td><code>\"</code></td>
<td><code>双引号</code></td>
</tr>
<tr>
<td><code>\\</code></td>
<td>反斜杠</td>
</tr>
<tr>
<td><code>\n</code></td>
<td>换行</td>
</tr>
<tr>
<td><code>\r</code></td>
<td><code>回车</code></td>
</tr>
<tr>
<td><code>\v</code></td>
<td>垂直制表符</td>
</tr>
<tr>
<td><code>\t</code></td>
<td>水平制表符</td>
</tr>
<tr>
<td><code>\b</code></td>
<td>退格</td>
</tr>
<tr>
<td><code>\f</code></td>
<td>换页</td>
</tr>
<tr>
<td><code>\uXXXX</code></td>
<td>unicode 码</td>
</tr>
<tr>
<td><code>\u{X}</code> ... <code>\u{XXXXXX}</code></td>
<td>unicode codepoint <span title="这是一个实验性的 API请尽量不要在生产环境中使用它。"><i class="icon-beaker"> </i></span></td>
</tr>
<tr>
<td><code>\xXX</code></td>
<td>Latin-1 字符(x小写)</td>
</tr>
</tbody>
</table>
<div class="note">和其他语言不同javascript 的字符串不区分单引号和双引号,所以不论是单引号还是双引号的字符串,上面的转义字符都能运行 。</div>
<h3 id="长字符串">长字符串</h3>
<p>有时,你的代码可能含有很长的字符串。你可能想将这样的字符串写成多行,而不是让这一行无限延长或着被编辑器折叠。有两种方法可以做到这一点。</p>
<p>其一,可以使用 + 运算符将多个字符串连接起来,如下所示:</p>
<pre><code>let longString = "This is a very long string which needs " +
"to wrap across multiple lines because " +
"otherwise my code is unreadable.";</code></code></pre>
<p>其二,可以在每行末尾使用反斜杠字符(“\”),以指示字符串将在下一行继续。确保反斜杠后面没有空格或任何除换行符之外的字符或缩进; 否则反斜杠将不会工作。 如下所示:</p>
<pre><code>let longString = "This is a very long string which needs \
to wrap across multiple lines because \
otherwise my code is unreadable.";</code></code></pre>
<p>使用这两种方式会创建相同的字符串。</p>
<dl>
</dl>
<h2 id="Description" name="Description">描述</h2>
<p>字符串对于保存可以以文本形式表示的数据非常有用。 一些常用的字符串操作有:查询<a href="Reference/Global_Objects/String/length">字符串长度</a>,使用<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/String_Operators"> + 和 += </a>运算符来构建和连接字符串,使用 <a href="Reference/Global_Objects/String/indexOf">indexOf</a> 方法检查某一子字符串在父字符串中的位置,又或是使用 <a href="Reference/Global_Objects/String/substring">substring</a> 方法提取从父字符串中提取子字符串。</p>
<h3 id="Character_access" name="Character_access">从字符串中获取单个字符</h3>
<p>获取字符串的某个字符有两种方法。 第一种是使用 <a href="Reference/Global_Objects/String/charAt" title="charAt() 方法从一个字符串中返回指定的字符。"><code>charAt</code></a> 方法:</p>
<pre><code class="language-javascript">return 'cat'.charAt(1); // returns "a"
</code></pre>
<p>另一种 (在ECMAScript 5中有所介绍) 是把字符串当作一个类似数组的对象,其中的每个字符对应一个数值索引:</p>
<pre><code class="language-javascript">return 'cat'[1]; // returns "a"
</code></pre>
<p>使用括号访问字符串不可以对其进行删除或添加,因为字符串对应未知的属性并不是可读或配置的。 (更多的信息请参阅 <a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty</code></a>。 )</p>
<h3 id="Comparing_strings" name="Comparing_strings">字符串比较</h3>
<p>熟练使用 C 语言的开发者经常使用 <code>strcmp</code> 函数来比较字符串但在 JavaScript 中,你只需要使用<a href="Reference/Operators/Comparison_Operators">比较操作符(&gt;/&lt;/&gt;=/&lt;=)</a></p>
<pre><code class="language-javascript">var a = "a";
var b = "b";
if (a &lt; b) // true
print(a + " is less than " + b);
else if (a &gt; b)
print(a + " is greater than " + b);
else
print(a + " and " + b + " are equal.");
</code></pre>
<p>使用从字符串实例继承而来的 <a href="Reference/Global_Objects/String/localeCompare" title="localeCompare() 方法返回一个数字来指示一个参考字符串是否在排序顺序前面或之后或与给定字符串相同。"><code>localeCompare</code></a> 方法也能达到同样的效果。 </p>
<h3 id="基本字符串和字符串对象的区别">基本字符串和<font face="Consolas, Liberation Mono, Courier, monospace">字符串</font>对象的区别</h3>
<p>请注意区分 JavaScript 字符串对象和基本字符串值 . ( 对于 <a href="Reference/Global_Objects/Boolean" title="Boolean对象是一个布尔值的对象包装器。"><code>Boolean</code></a><a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Numbers</code></a> 也同样如此.)</p>
<p>字符串字面量 (通过单引号或双引号定义) 和 直接调用 String 方法(没有通过 new 生成字符串对象实例)的字符串都是基本字符串。JavaScript会自动将基本字符串转换为字符串对象只有将基本字符串转化为字符串对象之后才可以使用字符串对象的方法。当基本字符串需要调用一个字符串对象才有的方法或者查询值的时候(基本字符串是没有这些方法的)JavaScript 会自动将基本字符串转化为字符串对象并且调用相应的方法或者执行查询。</p>
<pre><code class="language-javascript">var s_prim = "foo";
var s_obj = new String(s_prim);
console.log(typeof s_prim); // Logs "string"
console.log(typeof s_obj); // Logs "object"
</code></pre>
<p>当使用 <a href="Reference/Global_Objects/eval" title="执行指定代码之后的返回值。如果返回值为空返回undefined"><code>eval</code></a>时,基本字符串和<font face="Consolas, Liberation Mono, Courier, monospace">字符串</font>对象也会产生不同的结果。<code>eval</code> 会将基本字符串作为源代码处理; 而<font face="Consolas, Liberation Mono, Courier, monospace">字符串</font>对象则被看作对象处理, 返回对象。 例如:</p>
<pre><code class="language-javascript">s1 = "2 + 2"; // creates a string primitive
s2 = new String("2 + 2"); // creates a String object
console.log(eval(s1)); // returns the number 4
console.log(eval(s2)); // returns the string "2 + 2"
</code></pre>
<p>由于上述原因, 当一段代码在需要使用基本字符串的时候却使用了字符串对象就会导致执行失败(虽然一般情况下程序员们并不需要考虑这样的问题)。</p>
<p>利用 <a href="Reference/Global_Objects/String/valueOf" title="valueOf() 方法返回一个String对象的原始值primitive value。"><code>valueOf</code></a> 方法,我们可以将字符串对象转换为其对应的基本字符串。</p>
<pre><code class="language-javascript">console.log(eval(s2.valueOf())); // returns the number 4
</code></pre>
<div class="note"><strong>注意:</strong> 其他的将字符串对象转换成基本字符串的方法可以及参考 <a class="new" href="Typed_arrays/StringView" rel="nofollow" title="Typed_arrays/StringView"><code>StringView</code> a C-like representation of strings based on typed arrays</a>.</div>
<h2 id="属性">属性</h2>
<dl>
<dt><a href="Reference/Global_Objects/String/prototype" title="String.prototype 属性表示 String原型对象。"><code>String.prototype</code></a></dt>
<dd>可以为 String 对象增加新的属性。</dd>
</dl>
<h2 id="Methods" name="Methods">方法</h2>
<dl>
<dt><a href="Reference/Global_Objects/String/fromCharCode" title="静态 String.fromCharCode() 方法返回由指定的UTF-16代码单元序列创建的字符串。"><code>String.fromCharCode()</code></a>  </dt>
<dd> <strong>通过一串 Unicode 创建字符串。</strong></dd>
</dl>
<dl>
<dt><a href="Reference/Global_Objects/String/fromCodePoint" title="String.fromCodePoint() 静态方法返回使用指定的代码点序列创建的字符串。"><code>String.fromCodePoint()</code></a> <span title="这是一个实验性的 API请尽量不要在生产环境中使用它。"><i class="icon-beaker"> </i></span></dt>
<dd>通过一串 码点 创建字符串。</dd>
</dl>
<div>
<dl>
<dt><a href="Reference/Global_Objects/String/raw" title="String.raw() 是一个模板字符串的标签函数,它的作用类似于 Python 中的字符串前缀 r 和 C# 中的字符串前缀 @,是用来获取一个模板字符串的原始字面量值的。"><code>String.raw()</code></a> <span title="这是一个实验性的 API请尽量不要在生产环境中使用它。"><i class="icon-beaker"> </i></span></dt>
<dd>通过模板字符串创建字符串。</dd>
</dl>
</div>
<h2 id="字符串泛型方法">字符串泛型方法</h2>
<div class="warning">
<p>字符串泛型是<strong>非标准的</strong><strong>已弃用</strong><strong>并且会在不远的</strong><strong>将来删除</strong>。注意,你不能依靠他们的跨浏览器,而不使用下面提供的垫片。</p>
</div>
<p>应该避免在 Javascript 1.6 Firefox 浏览器的 JS 引擎)中使用(虽然也支持)将其他对象转化为字符的方法,因为方法并没有成为 ECMA 标准:</p>
<pre><code class="language-javascript">let num = 15;
console.log(String.replace(num, /5/, '2'));
</code></pre>
<p class="brush: js">移除字符串泛型的措施,参见 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_string_generics">Warning: String.x is deprecated; use String.prototype.x instead</a>.</p>
<p class="brush: js"><span style="background-color: #ffffff; color: #4d4e53; font-family: open sans,arial,sans-serif; font-size: 14px; line-height: 21px;"><a href="Reference/Global_Objects/Array#Array_generic_methods" title="JavaScript的 Array 对象是用于构造数组的全局对象,数组是类似于列表的高阶对象。">Generics</a> 在 Javascript 1.6 中同样支持<a href="Reference/Global_Objects/Array" title="JavaScript的 Array 对象是用于构造数组的全局对象,数组是类似于列表的高阶对象。"><code>Array</code></a></span></p>
<h2 id="String_instances" name="String_instances"><code>String</code> 实例</h2>
<h3 id="属性_2">属性</h3>
<p></p><dl>
<dt><code>String.prototype.constructor</code></dt>
<dd>用于创造对象的原型对象的特定的函数。</dd>
<dt><a href="Reference/Global_Objects/String/length" title="length 属性表示一个字符串的长度。"><code>String.prototype.length</code></a></dt>
<dd>返回了字符串的长度。</dd>
<dt><em>N</em></dt>
<dd>用于访问第N个位置的字符其中N是小于 <a href="Reference/Global_Objects/String/length" title="length 属性表示一个字符串的长度。"><code>length</code></a> 和 0之间的正整数。这些属性都是“只读”性质不能编辑。</dd>
</dl><p></p>
<p><span style="line-height: 1.5;"></span></p><h3 id="Methods_unrelated_to_HTML" name="Methods_unrelated_to_HTML">跟HTML无关的方法</h3>
<dl>
<dt><a href="Reference/Global_Objects/String/charAt" title="charAt() 方法从一个字符串中返回指定的字符。"><code>String.prototype.charAt()</code></a></dt>
<dd>返回特定位置的字符。</dd>
<dt><a href="Reference/Global_Objects/String/charCodeAt" title="charCodeAt() 方法返回0到65535之间的整数表示给定索引处的UTF-16代码单元 (在 Unicode 编码单元表示一个单一的 UTF-16 编码单元的情况下UTF-16 编码单元匹配 Unicode 编码单元。但在——例如 Unicode 编码单元 &gt; 0x10000 的这种——不能被一个 UTF-16 编码单元单独表示的情况下只能匹配 Unicode 代理对的第一个编码单元) 。如果你想要整个代码点的值,使用 codePointAt()。"><code>String.prototype.charCodeAt()</code></a></dt>
<dd>返回表示给定索引的字符的Unicode的值。</dd>
<dt><a href="Reference/Global_Objects/String/codePointAt" title="codePointAt() 方法返回 一个 Unicode 编码点值的非负整数。"><code>String.prototype.codePointAt()</code></a></dt>
<dd>返回使用UTF-16编码的给定位置的值的非负整数。</dd>
<dt><a href="Reference/Global_Objects/String/concat" title="concat() 方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。"><code>String.prototype.concat()</code></a></dt>
<dd>连接两个字符串文本,并返回一个新的字符串。</dd>
<dt><a href="Reference/Global_Objects/String/includes" title="includes() 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。"><code>String.prototype.includes()</code></a></dt>
<dd>判断一个字符串里是否包含其他字符串。</dd>
<dt><a href="Reference/Global_Objects/String/endsWith" title="endsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true 或 false。"><code>String.prototype.endsWith()</code></a></dt>
<dd>判断一个字符串的结尾是否包含其他字符串中的字符。</dd>
<dt><a href="Reference/Global_Objects/String/indexOf" title="indexOf() 方法返回调用  String 对象中第一次出现的指定值的索引开始在 fromIndex进行搜索。"><code>String.prototype.indexOf()</code></a></dt>
<dd>从字符串对象中返回首个被发现的给定值的索引值,如果没有找到则返回-1。</dd>
<dt><a href="Reference/Global_Objects/String/lastIndexOf" title="lastIndexOf() 方法返回指定值在调用该方法的字符串中最后出现的位置,如果没找到则返回 -1。从该字符串的后面向前查找从 fromIndex 处开始。"><code>String.prototype.lastIndexOf()</code></a></dt>
<dd>从字符串对象中返回最后一个被发现的给定值的索引值,如果没有找到则返回-1。</dd>
<dt><a href="Reference/Global_Objects/String/localeCompare" title="localeCompare() 方法返回一个数字来指示一个参考字符串是否在排序顺序前面或之后或与给定字符串相同。"><code>String.prototype.localeCompare()</code></a></dt>
<dd>返回一个数字表示是否引用字符串在排序中位于比较字符串的前面,后面,或者二者相同。</dd>
<dt><a href="Reference/Global_Objects/String/match" title="match() 方法检索返回一个字符串匹配正则表达式的的结果。"><code>String.prototype.match()</code></a></dt>
<dd>使用正则表达式与字符串相比较。</dd>
<dt><a href="Reference/Global_Objects/String/normalize" title="normalize() 方法会按照指定的一种 Unicode 正规形式将当前字符串正规化."><code>String.prototype.normalize()</code></a></dt>
<dd>返回调用字符串值的Unicode标准化形式。</dd>
<dt><a href="Reference/Global_Objects/String/padEnd" title="padEnd()  方法会用一个字符串填充当前字符串(如果需要的话则重复填充),返回填充后达到指定长度的字符串。从当前字符串的末尾(右侧)开始填充。"><code>String.prototype.padEnd()</code></a></dt>
<dd>在当前字符串尾部填充指定的字符串, 直到达到指定的长度。 返回一个新的字符串。</dd>
<dt><a href="Reference/Global_Objects/String/padStart" title="padStart() 方法用另一个字符串填充当前字符串(重复,如果需要的话),以便产生的字符串达到给定的长度。填充从当前字符串的开始(左侧)应用的。"><code>String.prototype.padStart()</code></a></dt>
<dd>
<p>在当前字符串头部填充指定的字符串, 直到达到指定的长度。 返回一个新的字符串。</p>
</dd>
<dt><a href="Reference/Global_Objects/String/quote" title='将字符串中包含的特殊字符进行转义(反斜杠),然后在字符串两边各加上一个双引号(")并返回,并不修改原字符串.'><code>String.prototype.quote()</code></a> <span class="icon-only-inline" title="This is an obsolete API and is no longer guaranteed to work."><i class="icon-trash"> </i></span></dt>
<dd>设置嵌入引用的引号类型。</dd>
<dt><a href="Reference/Global_Objects/String/repeat" title="repeat() 构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。"><code>String.prototype.repeat()</code></a></dt>
<dd>返回指定重复次数的由元素组成的字符串对象。</dd>
<dt><a href="Reference/Global_Objects/String/replace" title="replace() 方法返回一个由替换值replacement替换一些或所有匹配的模式pattern后的新字符串。模式可以是一个字符串或者一个正则表达式替换值可以是一个字符串或者一个每次匹配都要调用的函数。"><code>String.prototype.replace()</code></a></dt>
<dd>被用来在正则表达式和字符串直接比较,然后用新的子串来替换被匹配的子串。</dd>
<dt><a href="Reference/Global_Objects/String/search" title="search() 方法执行正则表达式和 String对象之间的一个搜索匹配。"><code>String.prototype.search()</code></a></dt>
<dd>对正则表达式和指定字符串进行匹配搜索,返回第一个出现的匹配项的下标。</dd>
<dt><a href="Reference/Global_Objects/String/slice" title="slice() 方法提取一个字符串的一部分,并返回一新的字符串。"><code>String.prototype.slice()</code></a></dt>
<dd>摘取一个字符串区域,返回一个新的字符串。</dd>
<dt><a href="Reference/Global_Objects/String/split" title="split() 方法使用指定的分隔符字符串将一个String对象分割成字符串数组以将字符串分隔为子字符串以确定每个拆分的位置。"><code>String.prototype.split()</code></a></dt>
<dd>通过分离字符串成字串,将字符串对象分割成字符串数组。</dd>
<dt><a href="Reference/Global_Objects/String/startsWith" title="startsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“开头”的,根据判断结果返回 true 或 false。"><code>String.prototype.startsWith()</code></a></dt>
<dd>判断字符串的起始位置是否匹配其他字符串中的字符。</dd>
<dt><a href="Reference/Global_Objects/String/substr" title="substr() 方法返回一个字符串中从指定位置开始到指定字符数的字符。"><code>String.prototype.substr()</code></a></dt>
<dd>通过指定字符数返回在指定位置开始的字符串中的字符。</dd>
<dt><a href="Reference/Global_Objects/String/substring" title="substring() 方法返回一个字符串在开始索引到结束索引之间的一个子集, 或从开始索引直到字符串的末尾的一个子集。"><code>String.prototype.substring()</code></a></dt>
<dd>返回在字符串中指定两个下标之间的字符。</dd>
<dt><a href="Reference/Global_Objects/String/toLocaleLowerCase" title="toLocaleLowerCase()方法根据任何特定于语言环境的案例映射,返回调用字符串值转换为小写的值。"><code>String.prototype.toLocaleLowerCase()</code></a></dt>
<dd>根据当前区域设置,将符串中的字符转换成小写。对于大多数语言来说,<a href="Reference/Global_Objects/String/toLowerCase" title="toLowerCase() 会将调用该方法的字符串值转为小写形式,并返回。"><code>toLowerCase</code></a>的返回值是一致的。</dd>
<dt><a href="Reference/Global_Objects/String/toLocaleUpperCase" title="toLocaleUpperCase() 使用本地化locale-specific的大小写映射规则将输入的字符串转化成大写形式并返回结果字符串。"><code>String.prototype.toLocaleUpperCase()</code></a></dt>
<dd>根据当前区域设置,将字符串中的字符转换成大写,对于大多数语言来说,<a href="Reference/Global_Objects/String/toUpperCase" title="toUpperCase() 将调用该方法的字符串值转换为大写形式,并返回。"><code>toUpperCase</code></a>的返回值是一致的。</dd>
<dt><a href="Reference/Global_Objects/String/toLowerCase" title="toLowerCase() 会将调用该方法的字符串值转为小写形式,并返回。"><code>String.prototype.toLowerCase()</code></a></dt>
<dd>将字符串转换成小写并返回。</dd>
<dt><a href="Reference/Global_Objects/String/toSource" title="toSource() 方法返回一个代表对象的源代码。"><code>String.prototype.toSource()</code></a> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span></dt>
<dd>返回一个对象文字代表着特定的对象。你可以使用这个返回值来创建新的对象。重写 <a href="Reference/Global_Objects/Object/toSource" title="toSource()方法返回一个表示对象源代码的字符串。"><code>Object.prototype.toSource</code></a> 方法。</dd>
<dt><a href="Reference/Global_Objects/String/toString" title="toString() 方法返回指定对象的字符串形式。"><code>String.prototype.toString()</code></a></dt>
<dd>返回用字符串表示的特定对象。重写 <a href="Reference/Global_Objects/Object/toString" title="toString() 方法返回一个表示该对象的字符串。"><code>Object.prototype.toString</code></a> 方法。</dd>
<dt><a href="Reference/Global_Objects/String/toUpperCase" title="toUpperCase() 将调用该方法的字符串值转换为大写形式,并返回。"><code>String.prototype.toUpperCase()</code></a></dt>
<dd>将字符串转换成大写并返回。</dd>
<dt><a href="Reference/Global_Objects/String/trim" title="trim() 方法会从一个字符串的两端删除空白字符。在这个上下文中的空白字符是所有的空白字符 (space, tab, no-break space 等) 以及所有行终止符字符(如 LFCR。"><code>String.prototype.trim()</code></a></dt>
<dd>从字符串的开始和结尾去除空格。参照部分 ECMAScript 5 标准。</dd>
<dt><a class="new" href="Reference/Global_Objects/String/trimStart" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>String.prototype.trimStart()</code></a></dt>
<dt><a href="Reference/Global_Objects/String/trimLeft" title="一个新字符串,表示从其开头(左端)剥离空格的调用字符串。"><code>String.prototype.trimLeft()</code></a> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span></dt>
<dd>从字符串的左侧去除空格。</dd>
<dt><a class="new" href="Reference/Global_Objects/String/trimEnd" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>String.prototype.trimEnd()</code></a></dt>
<dt><a href="Reference/Global_Objects/String/trimRight" title="trimRight() 方法从一个字符串的右端移除空白字符。"><code>String.prototype.trimRight()</code></a> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span></dt>
<dd>从字符串的右侧去除空格。</dd>
<dt><a href="Reference/Global_Objects/String/valueOf" title="valueOf() 方法返回一个String对象的原始值primitive value。"><code>String.prototype.valueOf()</code></a></dt>
<dd>返回特定对象的原始值。重写 <a href="Reference/Global_Objects/Object/valueOf" title="valueOf() 方法返回指定对象的原始值。"><code>Object.prototype.valueOf</code></a> 方法。</dd>
<dt><a href="Reference/Global_Objects/String/@@iterator" title="[@@iterator]() 方法返回一个新的Iterator对象它遍历字符串的代码点返回每一个代码点的字符串值。"><code>String.prototype[@@iterator]()</code></a></dt>
<dd>返回一个新的迭代器对象,该对象遍历字符串值的索引位置,将每个索引值作为字符串值返回。</dd>
</dl>
<h3 id="HTML_wrapper_methods" name="HTML_wrapper_methods">HTML wrapper methods</h3>
<p>下面的方法被限制使用因为只对可用的HTML标签和属性提供部分支持。</p>
<dl>
<dt><a href="Reference/Global_Objects/String/anchor" title="anchor() 方法创建一个 &lt;a&gt; HTML 锚元素被用作超文本靶标hypertext target。"><code>String.prototype.anchor()</code></a></dt>
<dd><code><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/a#attr-name">&lt;a name="name"&gt;</a></code> (hypertext target)</dd>
<dt><a href="Reference/Global_Objects/String/big" title="big()方法的作用是创建一个使字符串显示大号字体的&lt;big&gt;标签。"><code>String.prototype.big()</code></a> <span title="This deprecated API should no longer be used, but will probably still work."><i class="icon-thumbs-down-alt"> </i></span></dt>
<dd><a href="/zh-CN/docs/Web/HTML/Element/big" title="The HTML Big Element (&lt;big&gt;) 会使字体加大一号(例如从小号(small)到中号(medium),从大号(large)到加大(x-large)),最大不超过浏览器的最大字体。"><code>&lt;big&gt;</code></a></dd>
<dt><a href="Reference/Global_Objects/String/blink" title="blink()方法创建使字符串闪烁的 &lt;blink&gt; HTML 元素。"><code>String.prototype.blink()</code></a> <span title="This deprecated API should no longer be used, but will probably still work."><i class="icon-thumbs-down-alt"> </i></span></dt>
<dd><a href="/zh-CN/docs/Web/HTML/Element/blink" title="HTML Blink Element (&lt;blink&gt;)不是标准元素,它会使包含其中的文本闪烁。"><code>&lt;blink&gt;</code></a></dd>
<dt><a href="Reference/Global_Objects/String/bold" title="bold() 方法会创建 HTML 元素 “b”并将字符串加粗展示。"><code>String.prototype.bold()</code></a> <span title="This deprecated API should no longer be used, but will probably still work."><i class="icon-thumbs-down-alt"> </i></span></dt>
<dd><a href="/zh-CN/docs/Web/HTML/Element/b" title="HTML提醒注意Bring Attention To元素&lt;b&gt;用于吸引读者的注意到该元素的内容上如果没有另加特别强调。这个元素过去被认为是粗体Boldface元素并且大多数浏览器仍然将文字显示为粗体。尽管如此你不应将 &lt;b&gt; 元素用于显示粗体文字;替代方案是使用 CSS font-weight 属性来创建粗体文字。"><code>&lt;b&gt;</code></a></dd>
<dt><a href="Reference/Global_Objects/String/fixed" title="fixed()方法创建了一个标签元素将字符串包裹起来,从而让这个字符串里面的内容具有固定间距。"><code>String.prototype.fixed()</code></a> <span title="This deprecated API should no longer be used, but will probably still work."><i class="icon-thumbs-down-alt"> </i></span></dt>
<dd><a href="/zh-CN/docs/Web/HTML/Element/tt" title="HTML 电报文本元素 (&lt;tt&gt;) 产生一个内联元素,使用浏览器内置的 monotype 字体展示。这个元素用于给文本排版,使其等宽展示,就像电报那样。使用 &lt;code&gt; 元素来展示等宽文本可能更加普遍。"><code>&lt;tt&gt;</code></a></dd>
<dt><a href="Reference/Global_Objects/String/fontcolor" title="fontcolor()方法创建一个&lt;font&gt;的HTML元素让字符串被显示成指定的字体颜色。"><code>String.prototype.fontcolor()</code></a> <span title="This deprecated API should no longer be used, but will probably still work."><i class="icon-thumbs-down-alt"> </i></span></dt>
<dd><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/font#attr-color"><code>&lt;font color="<em>color</em>"&gt;</code></a></dd>
<dt><a href="Reference/Global_Objects/String/fontsize" title="The fontsize() method creates a &lt;font&gt; HTML element that causes a string to be displayed in the specified font size."><code>String.prototype.fontsize()</code></a> <span title="This deprecated API should no longer be used, but will probably still work."><i class="icon-thumbs-down-alt"> </i></span></dt>
<dd><a href="https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/font#attr-size"><code>&lt;font size="<em>size</em>"&gt;</code></a></dd>
<dt><a href="Reference/Global_Objects/String/italics" title="The italics() method creates an &lt;i&gt; HTML element that causes a string to be italic."><code>String.prototype.italics()</code></a> <span title="This deprecated API should no longer be used, but will probably still work."><i class="icon-thumbs-down-alt"> </i></span></dt>
<dd><a href="/zh-CN/docs/Web/HTML/Element/i" title="HTML元素 &lt;i&gt; 用于表现因某些原因需要区分普通文本的一系列文本。例如技术术语、外文短语或是小说中人物的思想活动等,它的内容通常以斜体显示。"><code>&lt;i&gt;</code></a></dd>
<dt><a href="Reference/Global_Objects/String/link" title="link() 方法创建一个 HTML 元素 &lt;a&gt; ,用该字符串作为超链接的显示文本,参数作为指向另一个 URL 的超链接。"><code>String.prototype.link()</code></a></dt>
<dd><a class="external" href="https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/a#attr-href"><code>&lt;a href="<em>url</em>"&gt;</code></a> (link to URL)</dd>
<dt><a href="Reference/Global_Objects/String/small" title="small() 方法的作用是创建一个使字符串显示小号字体的 &lt;small&gt; 标签。"><code>String.prototype.small()</code></a> <span title="This deprecated API should no longer be used, but will probably still work."><i class="icon-thumbs-down-alt"> </i></span></dt>
<dd><a href="/zh-CN/docs/Web/HTML/Element/small" title="HTML 中的元素將使文本的字体变小一号。(例如从大变成中等,从中等变成小,从小变成超小)。在HTML5中除了它的样式含义这个元素被重新定义为表示边注释和附属细则包括版权和法律文本。"><code>&lt;small&gt;</code></a></dd>
<dt><a href="Reference/Global_Objects/String/strike" title="strike()方法创建&lt;strike&gt; HTML 元素,使字符串展示为被删除的文本。"><code>String.prototype.strike()</code></a> <span title="This deprecated API should no longer be used, but will probably still work."><i class="icon-thumbs-down-alt"> </i></span></dt>
<dd><a href="/zh-CN/docs/Web/HTML/Element/strike" title="HTML &lt;strike&gt; 元素(或者 HTML 删除线元素)在文本上放置删除线。"><code>&lt;strike&gt;</code></a></dd>
<dt><a href="Reference/Global_Objects/String/sub" title="sub()方法创建一个 &lt;sub&gt; HTML 元素,使字符串展示为下标。"><code>String.prototype.sub()</code></a> <span title="This deprecated API should no longer be used, but will probably still work."><i class="icon-thumbs-down-alt"> </i></span></dt>
<dd><a href="/zh-CN/docs/Web/HTML/Element/sub" title="HTML &lt;sub&gt; 元素定义了一个文本区域,出于排版的原因,与主要的文本相比,应该展示得更低并且更小。"><code>&lt;sub&gt;</code></a></dd>
<dt><a href="Reference/Global_Objects/String/sup" title="sup()方法创建 一个&lt;sup&gt;HTML 元素,使字符串显示为上标。"><code>String.prototype.sup()</code></a> <span title="This deprecated API should no longer be used, but will probably still work."><i class="icon-thumbs-down-alt"> </i></span></dt>
<dd><a href="/zh-CN/docs/Web/HTML/Element/sup" title="HTML &lt;sup&gt; 元素定义了一个文本区域,出于排版的原因,与主要的文本相比,应该展示得更高并且更小。"><code>&lt;sup&gt;</code></a></dd>
</dl><p></p>
<h2 id="示例">示例 </h2>
<h3 id="将其他值转换成字符串">将其他值转换成字符串</h3>
<p>使用 String() 方法将其它对象转化为字符串可以被认为是一种更加安全的做法,虽然该方法底层使用的也是 toString() 方法,但是针对 null/undefined/symbolsString() 方法会有特殊的处理:</p>
<pre><code class="language-javascript">var outputStrings = [];
for (let i = 0, n = inputValues.length; i &lt; n; ++i) {
outputStrings.push(String(inputValues[i]));
}
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>ECMAScript 1st Edition.</td>
<td>Standard</td>
<td>Initial definition.</td>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/5.1/#sec-15.5" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">String</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td> </td>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-string-objects" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">String</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div><p></p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>0.2</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="sect1"> </h2>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="/zh-CN/docs/Web/API/DOMString" title="DOMString 是一个UTF-16字符串。由于JavaScript已经使用了这样的字符串所以DOMString 直接映射到 一个String。"><code>DOMString</code></a></li>
<li><a class="new" href="Typed_arrays/StringView" rel="nofollow" title="Typed_arrays/StringView"><code>StringView</code> a C-like representation of strings based on typed arrays</a></li>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/API/DOMString/Binary">Binary strings</a></li>
</ul>
</article>