どうも、ちょげ(@chogetarou)です。
charAt()で文字列(string)の先頭1文字を取得する方法を紹介します。
方法

charAt()で文字列(string)の最初の文字を取得するには、引数を使います。
まず、文字列からcharAt()を呼び出します。
そして、charAt()の引数に0を指定します。
char first = text.charAt(0);
上記のcharAt()は、呼び出した文字列の先頭1文字をchar型として取得します。
使用例
public class Main {
public static void main(String[] args) throws Exception {
String text = "Hello,World";
char first = text.charAt(0);
System.out.println(first);
}
}
出力:
H
コメント