CodeGym /Java Blog /ランダム /Java 文字列 charAt()
John Squirrels
レベル 41
San Francisco

Java 文字列 charAt()

ランダム グループに公開済み

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 string charAt()メソッドとは何か、およびその実装方法は理解できたと思います。気軽に練習して、さらにサポートが必要なときにいつでも戻ってください。楽しく学習しましょう!
コメント
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION