Java String charAt() 方法是什么?
Java 字符串类charAt()方法返回字符串指定索引处的字符。此指定索引作为参数提供,范围从 0 到 n-1,其中 n 表示字符串的长度。 句法public char charAt(int index)
参数 charAt ()方法接收一个正整数,因为长度永远不会低于 0。 返回 它始终返回指定索引的 char 值,但如果参数值为负数或大于字符串长度,则会引发异常。
Java String charAt() 方法示例
class Main {
public static void main(String[] args) {
String str = "Java charAt() method example";
// picking the A character of the string str at index 9 using charAt() method
System.out.println(str.charAt(9));
// picking the ( character of the string str at index 11 using charAt() method
System.out.println(str.charAt(11));
// picking the ) character of the string str at index 12 using charAt() method
System.out.println(str.charAt(12));
}
}
输出
БA ( )
GO TO FULL VERSION