下面是字符串的示例。原型charCodeAt()方法。
null
- 例子:
<script>
function
func() {
var
str =
'GEEKS'
;
var
value = str.charCodeAt(0);
document.write(value);
}
func();
</script>
- 输出:
71
charCodeAt街() 方法返回 Unicode字符集 在指定为参数的字符串中,索引处出现的字符的代码单位。该方法的语法如下所示:
str.charCodeAt(index)
论据 此方法的唯一参数是要使用Unicode的字符串中字符的索引。索引的范围是从 0 到 长度-1 .
返回值 此方法返回其索引作为参数提供给方法的字符的Unicode(范围在0到65535之间)。如果提供的索引超出范围,此方法将返回 楠 .
下面提供了上述方法的示例:
例1:
Input: var str = 'ephemeral'; print(str.charCodeAt(4)); Output: 109
在本例中,方法 charCodeAt() 从索引处的字符串中提取字符 4. .因为这个角色 M 因此,此方法将Unicode序列返回为 109 .
例2:
Input: var str = 'ephemeral'; print(str.charCodeAt(20)); Output: NaN
在本例中,方法 charCodeAt() 从索引处的字符串中提取字符 20 .由于索引超出了字符串的界限,因此此方法返回的答案为 楠 .
下面提供了上述方法的代码:
项目1:
<script> // JavaScript to illustrate charCodeAt() method function func() { var str = 'ephemeral' ; // Finding the code of the character at // given index var value = str.charCodeAt(4); document.write(value); } func(); </script> |
输出:
109
项目2:
<script> // JavaScript to illustrate charCodeAt() method function func() { var str = 'ephemeral' ; // Finding the code of the character // at given index var value = str.charCodeAt(20); document.write(value); } func(); </script> |
输出:
NaN
支持的浏览器:
- 铬1及以上
- 边缘12及以上
- Firefox 1及以上版本
- Internet Explorer 4及以上版本
- safari 1及以上
- 歌剧4及以上
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END