indexOf() メソッド
メニューを表示するにはスワイプしてください
文字列内で指定した文字のインデックスを見つける方法
特定の文字が最初に現れる位置(インデックス)を調べるには、String クラスの indexOf() メソッドを使用。このメソッドはオーバーロードされており、いくつかの実装が存在。それぞれの動作について説明。
indexOf(String str)
indexOf(String str) - このメソッドでは、パラメータで検索対象を指定。ダブルクォーテーション("")で囲んだ1文字、文字列、または単語を指定可能。このメソッドは、指定したパラメータが文字列内で最初に現れるインデックスを返す。例えば、文字列 "l" から "Hello" のインデックスを取得する例:
Main.java
12345678910111213package com.example; // do not modify the code below this comment public class Main { public static void main(String[] args) { String hello = "Hello"; // find the index of the first appearance of the letter "l" int index = hello.indexOf("l"); // output the index of the first occurrence of "l" System.out.println("First appearance of the letter 'l' is on index " + index); } }
上記の例では、変数 index を indexOf("l") メソッドで初期化しました。
この変数には、文字列 "l" における文字 "Hello" の最初の出現位置が格納されています。
indexOf(String str, int fromIndex)
indexOf(String str, int fromIndex) - 最初のパラメータはこれまでと同じく検索対象を指定します。
2番目のパラメータは検索を開始するインデックスを示します。
例えば、単語 "Hello" には文字 "l" が2回出現し、2回目の出現位置を調べたい場合です。
最初の例から、1回目の出現はインデックス 2 であることが分かっています。そこで、fromIndex パラメータを3に設定し、2回目の文字 "l" のインデックスを取得します:
Main.java
123456789101112131415package com.example; // do not modify the code below this comment public class Main { public static void main(String[] args) { String hello = "Hello"; // find the index of the first appearance of the letter "l" int firstIndex = hello.indexOf("l"); // find the index of the second appearance of the letter "l", starting from the position after the first "l" int secondIndex = hello.indexOf("l", firstIndex + 1); // output the index of the second occurrence of "l" System.out.println("Second appearance of the letter 'l' is on index " + secondIndex); } }
上記の例では、より実践的なアプローチを採用しています。指定した文字の最初の出現位置を利用し、そのインデックスに1を加えた位置から2番目の文字を検索しています。これにより、目的の文字や文字列の最初の出現位置の次のインデックスから検索を開始します。
また、パラメータ String str と int fromIndex は入れ替えて使用できます。
さらに、indexOf() メソッドが指定した文字や文字列を見つけられなかった場合、値 -1 を返すことにも注意が必要です。例えば:
Main.java
12345678910111213package com.example; // do not modify the code below this comment public class Main { public static void main(String[] args) { String hello = "Hello"; // find the index of the letter "d" in the string int index = hello.indexOf("d"); // output the index, if the specified letter is not found, the value will be -1 System.out.println("Value of index, if it does not find a specified letter: " + index); } }
文字 "d" を "Hello" という文字列で検索した結果、見つからず、メソッドは -1 を返しました。これは条件設定やループからの脱出ポイントの作成に役立ちます。
文字列内の最後のインデックスの検索方法
String には、文字列の末尾から検索できるメソッドもあります。このメソッドは lastIndexOf() と呼ばれます。同じ原理で動作します。次に、文字列 "l" で文字 "Hello" の最後の出現位置を検索する例を示します:
Main.java
12345678910111213package com.example; // do not modify the code below this comment public class Main { public static void main(String[] args) { String hello = "Hello"; // find the last index of the letter "l" int lastIndex = hello.lastIndexOf("l"); // output the last index where the letter "l" appears System.out.println("Last index of the letter 'l' : " + lastIndex); } }
ここでは、結果として 3 を取得しました。これは文字 "l" の最後の出現位置のインデックスを表します。
フィードバックありがとうございます!
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください