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

红太狼 2022-09-26 01:53 292阅读 0赞

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 条评论,292人围观)

还没有评论,来说两句吧...

相关阅读