CodeGym /Java Blog /Toto sisi /Java 字串 charAt()
John Squirrels
等級 41
San Francisco

Java 字串 charAt()

在 Toto sisi 群組發布

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 ( )

結論

我們希望現在您了解什麼是 Java 字串charAt()方法以及如何實作它。請隨意練習,並在需要更多幫助時返回。快樂學習!
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION