1,概述
charAt(int index)方法是一个能够用来检索特定索引下的字符的String实例的方法.
charAt()方法返回指定索引位置的char值。索引范围为0~length()-1.
如: str.charAt(0)检索str中的第一个字符,str.charAt(str.length()-1)检索最后一个字符.
2,语法
方法声明 public char charAt(int index)
入口参数:index是char的索引值,index必须是表示字符串中某个位置的数字,即字符在字符串中的下标。
提示:字符串中第一个字符的下标是 0。如果参数 index 不在 0 与 string.length 之间,该方法将返回一个空字符串。
3,实例
使用charAt函数获取字符串str中的索引值为2的char值,并将结果赋值给int变量strLower。
String str= "Iloveyou";
int strLower=str.charAt(2);
javascript实例
如何使用charAt()从某个字符串中取得具体的字符。
<
html
>
<head> 如何使用charAt()从某个字符串中取得具体的字符</head>
<
body
>
<
script type
=
"text/javascript"
>
var str="Helloworld!"
document.write("Thefirstcharacteris:"+str.charAt(0)+"<
br
/>");
document.write("Thesecondcharacteris:"+str.charAt(1)+"<
br
/>");
document.write("Thethirdcharacteris:"+str.charAt(2));
</
script
>
</
body
>
</
html
>