どうも、ちょげ(@chogetarou)です。
文字列(string)の特定の文字が何文字目か検索する方法を紹介します。
方法

文字列(string)の特定の文字の位置を検索するには、indexOf()を使います。
まず、文字列からindexOf()を呼び出します。
そして、indexOf()の引数に位置を検索する文字を指定します。
//text=対象の文字列, char=文字
int result = text.indexOf(char);
上記のindexOf()は、呼び出した文字列の引数の文字の位置を取得します。
(文字の位置は、先頭から「0、1、2・・・」となります。)
使用例
public class Main {
public static void main(String[] args) throws Exception {
String text = "Hello,World";
int result = text.indexOf("W");
System.out.println(result);
}
}
出力:
6
コメント