本文主要是介绍JS中string方法中常用方法之一:String.prototype.charAt(),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 ''
这篇关于JS中string方法中常用方法之一:String.prototype.charAt()的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!