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