Java String indexOf example

String's indexOf method is used to find out index of any character or sub string.

indexOf method can take char or string as argument and also you can provide fromIndex(Index after which you want to use indexOf method) also.

Methods:

public int indexOf(int ch)
returns index of first occurrence of character within String

public int indexOf(int ch,int fromIndex)
returns index of first occurrence of character within String, starting from specific index "fromIndex"

public int indexOf(String)
returns index of first occurrence of substring within String

public int indexOf(int ch,int fromIndex)
returns index of first occurrence of substring within String, starting from specific index "fromIndex"

All indexOf methods return -1 if char or substring is not present in the String

String indexOf Example:

package org.arpit.java2blog;

public class StringIndexOfExample {
 /*
  * @ Author : Arpit Mandliya
  */
 public static void main(String args[]) {
  
  String str="Hello world from java2blog";
  // Using index of method with character
  System.out.println("---------------------------------------------------");
  System.out.println("Using indexOf method with character");
  System.out.println("---------------------------------------------------");
  
  System.out.println("Index of character b in \"Hello world from java2blog\" is : "+str.indexOf('b'));
  System.out.println();
  
  String str1="from";
  System.out.println("---------------------------------------------------");
  System.out.println("Using indexOf method with fromIndex with character");
  System.out.println("---------------------------------------------------");
  System.out.println("Index of character o from index 6 in \"Hello world from java2blog\" is : "+str.indexOf('o',6));
  System.out.println();
  
  // Using index of method with String
  System.out.println("---------------------------------------------------");
  System.out.println("Using indexOf method with String");
  System.out.println("---------------------------------------------------");
  
  System.out.println("Index of String \"from\" in \"Hello world from java2blog\" is : "+str.indexOf(str1));
  System.out.println();
  
  String howDo="How do you do";
  System.out.println("---------------------------------------------------");
  System.out.println("Using indexOf method with fromIndex with String");
  System.out.println("---------------------------------------------------");
  System.out.println("Index of String \"do\" from index 9 in \"How do you do\" is : "+howDo.indexOf("do",9));
  System.out.println();
  
  System.out.println("---------------------------------------------------");
  System.out.println("If String or char is not available");
  System.out.println("---------------------------------------------------");
  System.out.println("Index of \"from\" in \"How do you do\" is : "+howDo.indexOf("from"));
  
  
 }
}

When you run above program, you will get below output:
--------------------
Using indexOf method with character
--------------------
Index of character b in "Hello world from java2blog" is : 22

--------------------
Using indexOf method with fromIndex with character
--------------------
Index of character o from index 6 in "Hello world from java2blog" is : 7

--------------------
Using indexOf method with String
--------------------
Index of String "from" in "Hello world from java2blog" is : 12

--------------------
Using indexOf method with fromIndex with String
--------------------
Index of String "do" from index 9 in "How do you do" is : 11

--------------------
If String or char is not available
--------------------
Index of "from" in "How do you do" is : -1


Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012