JavaでのindexOfメソッドの使い方|基本から応用までコード付きで解説

- システム
エンジニア - JavaでのindexOfメソッドはどのように使うのでしょうか。
- プロジェクト
マネージャー - 検索対象の文字列の中で指定文字列が最初に出現するインデックスを返却するので、StringやArrayList、StringBufferなどに対して使用できます。
JavaでのindexOfメソッドの使い方とは?
今回は、JavaでのindexOfメソッドの使い方について説明します。
indexOfメソッドは、検索対象の文字列の中で、指定文字列が最初に出現するインデックスを返却します。StringやArrayList、StringBufferなどに対して使用できます。
また、指定文字列が最後に出現するインデックスを返却するlastIndexOfメソッドも使用できます。
JavaでのindexOfメソッドの使い方に興味のある方はぜひご覧ください。
基本的な使い方
JavaでのindexOfメソッドの基本的な使い方を紹介します。indexOfメソッドには、以下の4つのオーバーロードメソッドがあります。
1 2 3 4 |
public int indexOf(String str) public int indexOf(String str, int fromIndex) public int indexOf(int ch) public int indexOf(int ch, int fromIndex) |
第2引数を与えることで、その位置から後ろを検索します。指定文字が存在しない場合は、-1を返却します。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class Main { public static void main(String[] args) throws Exception { String str = ""This is indexOf method sample code. This is indexOf method sample code.""; int index = str.indexOf(""is""); System.out.println(index); // 2 index = str.indexOf(""is"", 30); System.out.println(index); // 38 index = str.indexOf('h'); System.out.println(index); // 1 index = str.indexOf('h', 10); System.out.println(index); // 19 index = str.indexOf('z'); System.out.println(index); // -1 } } |
実行結果は以下のようになります。
1 2 3 4 5 |
2 38 1 19 -1 |
対象文字列がnullの場合、NullPointerExceptionが発生します。
1 2 3 4 5 6 7 8 |
public class Main { public static void main(String[] args) throws Exception { String str = null; int index = str.indexOf(""is""); System.out.println(index); } } |
ArrayListでindexOf
Javaでは、ArrayListの要素に対してもindexOfメソッドを使用できます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.ArrayList; public class Main { public static void main(String[] args) throws Exception { ArrayList<String> array = new ArrayList<String>(); array.add(""item1""); array.add(""item2""); array.add(""item3""); array.add(""item4""); array.add(""item5""); int index = array.indexOf(""item1""); System.out.println(index); // 0 index = array.indexOf(""item6""); System.out.println(index); // -1 } } |
実行結果は以下のようになります。
1 2 |
0 -1 |
このように、JavaではArrayListの要素に対してもindexOfメソッドを使用できます。
StringBufferでindexOf
Javaでは、StringBufferに対してもindexOfメソッドを使用できます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 |
public class Main { public static void main(String[] args) throws Exception { StringBuffer buff = new StringBuffer(""This is indexOf method sample code. This is indexOf method sample code.""); int index = buff.indexOf(""is""); System.out.println(index); // 2 index = buff.indexOf(""is"", 30); System.out.println(index); // 38 } } |
実行結果は以下のようになります。
1 2 |
2 38 |
このように、JavaではStringBufferに対してもindexOfメソッドを使用できます。
複数要素
文字列内に指定文字が複数存在する場合のindex取得方法を紹介します。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Main { public static void main(String[] args) throws Exception { String str = ""This is indexOf method sample code.""; int index; for (int i = 0; i < str.length(); i++) { index = str.indexOf(""s"", i); if (index != -1) { System.out.println(""「s」のindex: "" + index); i = index; } } } } |
実行結果は以下のようになります。
1 2 3 |
「s」のindex: 3 「s」のindex: 6 「s」のindex: 23 |
indexOfの第2引数をずらしながら検索しています。
lastIndexOf
Javaでは、指定文字列が最後に出現するインデックスを返却するlastIndexOfメソッドが使用できます。
StringやArrayList、StringBufferなどに対して使用できます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.ArrayList; public class Main { public static void main(String[] args) throws Exception { // Stringに対する検索 String str = ""This is indexOf method sample code. This is indexOf method sample code.""; int index = str.lastIndexOf(""is""); System.out.println(index); // 41 // ArrayListに対する検索 ArrayList<String> array = new ArrayList<String>(); array.add(""item1""); array.add(""item2""); array.add(""item1""); array.add(""item4""); array.add(""item5""); index = array.lastIndexOf(""item1""); System.out.println(index); // 2 // StringBufferに対する検索 StringBuffer buff = new StringBuffer(""This is indexOf method sample code. This is indexOf method sample code.""); index = buff.lastIndexOf(""is""); System.out.println(index); // 41 } } |
実行結果は以下のようになります。
1 2 3 |
41 2 41 |
このように、Javaでは、指定文字列が最後に出現するインデックスを返却するlastIndexOfメソッドが使用できます。
- システム
エンジニア - StringやArrayList、StringBufferなどに対して使用する以外にも、指定文字列が最後に出現するインデックスを返却するlastIndexOfメソッドも使用できるのですね。
- プロジェクト
マネージャー - indexOfメソッドの使い方が理解できたら、自分でソースコードを書いてより理解を深めましょう。
まとめ
いかがでしたでしょうか。JavaでのindexOfメソッドの使い方について説明しました。
indexOfメソッドは、検索対象の文字列の中で、指定文字列が最初に出現するインデックスを返却します。
StringやArrayList、StringBufferなどに対して使用できます。また、指定文字列が最後に出現するインデックスを返却するlastIndexOfメソッドも使用できます。
ぜひご自身でソースコードを書いて、理解を深めてください。
FEnetJava・Javaコラムは株式会社オープンアップシステムが運営しています。
株式会社オープンアップシステムはこんな会社です
秋葉原オフィスには株式会社オープンアップシステムをはじめグループのIT企業が集結!
数多くのエンジニアが集まります。

-
スマホアプリから業務系システムまで
スマホアプリから業務系システムまで開発案件多数。システムエンジニア・プログラマーとしての多彩なキャリアパスがあります。
-
充実した研修制度
毎年、IT技術のトレンドや社員の要望に合わせて、カリキュラムを刷新し展開しています。社内講師の丁寧なサポートを受けながら、自分のペースで学ぶことができます。
-
資格取得を応援
スキルアップしたい社員を応援するために資格取得一時金制度を設けています。受験料(実費)と合わせて資格レベルに合わせた最大10万円の一時金も支給しています。
-
東証プライム上場企業グループ
オープンアップシステムは東証プライム上場「株式会社オープンアップグループ」のグループ企業です。
安定した経営基盤とグループ間のスムーズな連携でコロナ禍でも安定した雇用を実現させています。
株式会社オープンアップシステムに興味を持った方へ
株式会社オープンアップシステムでは、開発系エンジニア・プログラマを募集しています。
年収をアップしたい!スキルアップしたい!大手の上流案件にチャレンジしたい!
まずは話だけでも聞いてみたい場合もOK。お気軽にご登録ください。


Java新着案件New Job
官公庁向け業務システム開発/Java/東京都千代田区/【WEB面談可】/テレワーク
月給39万~44万円東京都千代田区(永田町駅)販売管理システム開発/Java/東京都中央区/【WEB面談可】/テレワーク
月給49万~55万円東京都中央区(京橋駅)生命保険会社向けシステム開発/Java/東京都千代田区/【WEB面談可】/テレワーク
月給42万~48万円東京都千代田区(大手町駅)社会保険システムのパッケージ開発/Java/東京都港区/【WEB面談可】/テレワーク
月給42万~48万円東京都港区(新橋駅)金融機関向けシステム更改/Java/東京都江東区/【WEB面談可】/テレワーク
月給46万~51万円東京都江東区(豊洲駅)大手通信会社者向けWebシステム改修/Java/東京都港区/【WEB面談可】/テレワーク
月給42万~48万円東京都港区(品川駅)