JS中string方法中常用方法之一:String.prototype.charAt()

来源:互联网 发布:好莱坞爱情电影知乎 编辑:程序博客网 时间:2024/06/05 15:55
String.prototype.charAt()返回字符串中指定位置的字符。
语法:str.charAt(index)(index 是0 到 字符串长度-1 的一个整数)

例子:输出字符串中不同位置的字符
//下例输出字符串 "Brave new world" 不同位置处的字符

var anyString = "Brave new world";

console.log("The character at index 0   is '" + anyString.charAt(0)   + "'");
console.log("The character at index 1   is '" + anyString.charAt(1)   + "'");
console.log("The character at index 2   is '" + anyString.charAt(2)   + "'");
console.log("The character at index 3   is '" + anyString.charAt(3)   + "'");
console.log("The character at index 4   is '" + anyString.charAt(4)   + "'");
console.log("The character at index 999 is '" + anyString.charAt(999) + "'");
//上面代码的输出为:
The character at index 0 is 'B'
The character at index 1 is 'r'
The character at index 2 is 'a'
The character at index 3 is 'v'
The character at index 4 is 'e'
The character at index 999 is ''
0 0
原创粉丝点击