charAt()
public char charAt(int index)
charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。
参数:
index- 值的索引char。
返回:
char此字符串的指定索引处的值。第一个char值位于 index 处0。
抛出:
IndexOutOfBoundsException- 如果index 参数为负数或不小于此字符串的长度。
实例
public class Test {
public static void main(String args[]) {
String s = "www.hello.com";
char result = s.charAt(6);
System.out.println(result);
}
}
以上程序执行结果为:
l
源码
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {//小于0或大于长度
throw new StringIndexOutOfBoundsException(index);
}
return value[index];//字符数组
}
底层采用的是数组
浙公网安备 33010602011771号