diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/string/charcodeat/index.html')
| -rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/string/charcodeat/index.html | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/charcodeat/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/charcodeat/index.html new file mode 100644 index 0000000000..2fa6e19305 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/string/charcodeat/index.html @@ -0,0 +1,161 @@ +--- +title: String.prototype.charCodeAt() +slug: Web/JavaScript/Reference/Global_Objects/String/charCodeAt +tags: + - JavaScript + - String + - Unicode + - 参考 + - 字符串 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/String/charCodeAt +--- +<div>{{JSRef}}</div> + +<p><code><strong>charCodeAt()</strong></code> 方法返回 <code>0</code> 到 <code>65535</code> 之间的整数,表示给定索引处的 UTF-16 代码单元</p> + +<div>{{EmbedInteractiveExample("pages/js/string-charcodeat.html", "shorter")}}</div> + + + +<p>UTF-16 编码单元匹配能用一个 UTF-16 编码单元表示的 Unicode 码点。如果 Unicode 码点不能用一个 UTF-16 编码单元表示(因为它的值大于<code>0xFFFF</code>),则所返回的编码单元会是这个码点代理对的第一个编码单元) 。如果你想要整个码点的值,使用 {{jsxref("Global_Objects/String/codePointAt", "codePointAt()")}}。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate"><code><em>str</em>.charCodeAt(<em>index</em>)</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>index</code></dt> + <dd>一个大于等于 <code>0</code>,小于字符串长度的整数。如果不是一个数值,则默认为 <code>0</code>。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>指定 <code>index</code> 处字符的 UTF-16 代码单元值的一个数字;如果 <code>index</code> 超出范围,<code>charCodeAt()</code> 返回 {{jsxref("Global_Objects/NaN", "NaN")}}。</p> + +<h2 id="描述">描述</h2> + +<p>Unicode 码点(code points)的范围从 <code>0</code> 到 <code>1114111</code> <code>(0x10FFFF)</code>。开头的 128 个 Unicode 编码单元和 ASCII 字符编码一样。(关于 Unicode 的更多信息,可查看 <a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Unicode">JavaScript Guide</a>。)</p> + +<div class="blockIndicator note"> +<p><strong>注意:</strong>,<code>charCodeAt</code> 总是返回一个小于 65,536 的值。这是因为高位编码单元(higher code point)使用一对(低位编码 lower valued)代理伪字符("surrogate" pseudo-characters)来表示,从而构成一个真正的字符。</p> + +<p>因此,为了检查(或重现)<code>65536</code> 及以上编码字符的完整字符,需要在获取 <code>charCodeAt(i)</code> 的值的同时获取 <code>charCodeAt(i+1)</code> 的值(如同用两个字母操纵一个字符串),或者改为获取 <code>codePointAt(i)</code> 的值。参看下面例 2 和例 3。</p> +</div> + +<p>如果指定的 <code>index</code> 小于 <code>0</code> 、等于或大于字符串的长度,则 <code>charCodeAt</code> 返回 {{jsxref("Global_Objects/NaN", "NaN")}}。</p> + +<p>向后兼容:在历史版本中(如 JavaScript 1.2),<code>charCodeAt</code> 返回一个数字,表示给定 index 处字符的 ISO-Latin-1 编码值。ISO-Latin-1 编码集范围从 <code>0</code> 到 <code>255</code>。开头的 <code>0</code> 到 <code>127</code> 直接匹配 ASCII 字符集。</p> + +<h2 id="Examples" name="Examples">示例</h2> + +<h3 id="Example_Using_charCodeAt" name="Example:_Using_charCodeAt">使用 <code>charCodeAt()</code></h3> + +<p>下例介绍了不同索引情况下返回的 Unicode 值:</p> + +<pre class="brush: js notranslate">"ABC".charCodeAt(0) // returns 65:"A" + +"ABC".charCodeAt(1) // returns 66:"B" + +"ABC".charCodeAt(2) // returns 67:"C" + +"ABC".charCodeAt(3) // returns NaN</pre> + +<h3 id="使用_charCodeAt_修复字符串中出现的未知的非基本多语言范围(非BMP,non-Basic-Multilingual-Plane)字符">使用 <code>charCodeAt()</code> 修复字符串中出现的未知的非基本多语言范围(非BMP,non-Basic-Multilingual-Plane)字符</h3> + +<p>这段代码可以被用在 for 循环和其他类似语句中,当在指定引索之前不确定是否有非BMP字符存在时。</p> + +<pre class="brush:js notranslate">function fixedCharCodeAt (str, idx) { + // ex. fixedCharCodeAt ('\uD800\uDC00', 0); // 65536 + // ex. fixedCharCodeAt ('\uD800\uDC00', 1); // false + idx = idx || 0; + var code = str.charCodeAt(idx); + var hi, low; + + // High surrogate (could change last hex to 0xDB7F to treat high + // private surrogates as single characters) + if (0xD800 <= code && code <= 0xDBFF) { + hi = code; + low = str.charCodeAt(idx+1); + if (isNaN(low)) { + throw 'High surrogate not followed by low surrogate in fixedCharCodeAt()'; + } + return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000; + } + if (0xDC00 <= code && code <= 0xDFFF) { // Low surrogate + // We return false to allow loops to skip this iteration since should have + // already handled high surrogate above in the previous iteration + return false; + /*hi = str.charCodeAt(idx-1); + low = code; + return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;*/ + } + return code; +} +</pre> + +<h3 id="使用_charCodeAt_修复字符串中出现的已知的非BMP字符">使用 <code>charCodeAt()</code> 修复字符串中出现的已知的非BMP字符</h3> + +<pre class="brush:js notranslate">function knownCharCodeAt (str, idx) { + str += ''; + var code, + end = str.length; + + var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g; + while ((surrogatePairs.exec(str)) != null) { + var li = surrogatePairs.lastIndex; + if (li - 2 < idx) { + idx++; + } + else { + break; + } + } + + if (idx >= end || idx < 0) { + return NaN; + } + + code = str.charCodeAt(idx); + + var hi, low; + if (0xD800 <= code && code <= 0xDBFF) { + hi = code; + low = str.charCodeAt(idx+1); + // Go one further, since one of the "characters" is part of a surrogate pair + return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000; + } + return code; +} + +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-string.prototype.charcodeat', 'String.prototype.charCodeAt')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p class="hidden">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.charCodeAt")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{jsxref("String.fromCharCode()")}}</li> + <li>{{jsxref("String.prototype.charAt()")}}</li> + <li>{{jsxref("String.fromCodePoint()")}}</li> + <li>{{jsxref("String.prototype.codePointAt()")}}</li> +</ul> |
