String Searching Methods with Examples

String Searching Methods Overview

The String class provides two methods that allow you to search a string for a specified character or substring:
  • indexOf( ) Searches for the first occurrence of a character or substring.
  • lastIndexOf( ) Searches for the last occurrence of a character or substring. These two methods are overloaded in several different ways. In all cases, the methods return the index at which the character or substring was found, or –1 on failure.
To search for the first occurrence of a character, use
int indexOf(int ch)
To search for the last occurrence of a character, use
int lastIndexOf(int ch)
Here, ch is the character being sought.
To search for the first or last occurrence of a substring, use
int indexOf(String str)
int lastIndexOf(String str)
Here, str specifies the substring.
You can specify a starting point for the search using these forms:
int indexOf(int ch, int startIndex)
int lastIndexOf(int ch, int startIndex)
int indexOf(String str, int startIndex)
int lastIndexOf(String str, int startIndex)
Here, startIndex specifies the index at which point the search begins. For indexOf( ), the search runs from startIndex to the end of the string. For lastIndexOf( ), the search runs from startIndex to zero.
The following example shows how to use the various index methods to search inside of a String:
// Demonstrate indexOf() and lastIndexOf().
class indexOfDemo {
    public static void main(String args[]) {
         String s = "Now is the time for all good men " + "to come to the aid of their country.";
         System.out.println(s);
         System.out.println("indexOf(t) = " + s.indexOf('t'));
         System.out.println("lastIndexOf(t) = " + s.lastIndexOf('t'));
         System.out.println("indexOf(the) = " + s.indexOf("the"));
         System.out.println("lastIndexOf(the) = " + s.lastIndexOf("the"));
         System.out.println("indexOf(t, 10) = " + s.indexOf('t', 10));
         System.out.println("lastIndexOf(t, 60) = " + s.lastIndexOf('t', 60));
         System.out.println("indexOf(the, 10) = " + s.indexOf("the", 10));
         System.out.println("lastIndexOf(the, 60) = " + s.lastIndexOf("the", 60));
    }
}
Here is the output of this program:
Now is the time for all good men to come to the aid of their country.
indexOf(t) = 7
lastIndexOf(t) = 65
indexOf(the) = 7
lastIndexOf(the) = 55
indexOf(t, 10) = 11
lastIndexOf(t, 60) = 55
indexOf(the, 10) = 44
lastIndexOf(the, 60) = 55
Let's understand indexOf() and lastIndexOf() methods and their different forms in depth.

String Searching Methods with Examples

indexOf()

There are 4 types of an indexOf method in java. The signature of indexOf methods are given below:
  • indexOf(int ch) - Returns the index within this string of the first occurrence of the specified character.
  • indexOf(int ch, int fromIndex) - Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
  • indexOf(String str) - Returns the index within this string of the first occurrence of the specified substring.
  • indexOf(String str, int fromIndex) - Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
Example: This program demonstrates the example of all the 4 indexOf() methods.
public class IndexOfExample {
    public static void main(String[] args) {
         String str = "javaguides";
  
        // method 1
        int index = str.indexOf("java");
        System.out.println(index);
  
        // Remember index starts with 0 so count from 0
        System.out.println("index of guides :: " + str.indexOf("guides"));
  
        System.out.println(" index of des :: " + str.indexOf("des"));
  
        // method 2
        System.out.println(str.indexOf('s'));
  
        // method 3
        System.out.println(str.indexOf('g', 0));
  
        // method 4
        System.out.println(str.indexOf("guides", 3));
    }
}
Output:
0
index of guides :: 4
 index of des :: 7
9
4
4

lastIndexOf() methods

There are 4 types of a lastIndexOf method in java. The signature of lastIndexOf methods are given below:
  • lastIndexOf(int ch) - Returns the index within this string of the last occurrence of the specified character.
  • lastIndexOf(int ch, int fromIndex) - Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.
  • lastIndexOf(String str) - Returns the index within this string of the last occurrence of the specified substring.
  • lastIndexOf(String str, int fromIndex) - Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.
The main usage of lastIndexOf( ) - Searches for the last occurrence of a character or substring.
Example: This program demonstrate the usage of all 4 lastIndexOf() methods.
public class LastIndexOfExample {
    public static void main(String[] args) {
         String str = "javaguides";
  
         // method1
         int lastIndexOf = str.lastIndexOf('s');
         System.out.println(" last index of given character 's' in' "
         + " "+ str+"' ::  " + lastIndexOf);
  
        // method 2
        lastIndexOf = str.lastIndexOf("guides");
        System.out.println(" last index of given string 'guides' in' "
        + " "+ str+"' ::  " + lastIndexOf);
  
        // method 3
        lastIndexOf = str.lastIndexOf("guides", 4);
        System.out.println(" last index of guides in given  string "
        + " "+ str+" and from index  " + lastIndexOf);
  
        // method 4
        lastIndexOf = str.lastIndexOf('g', str.length());
        System.out.println(" last index of given char ::  " + lastIndexOf);
    }
}
Output:
 last index of given character 's' in'  javaguides' ::  9
 last index of given string 'guides' in'  javaguides' ::  4
 last index of guides in given  string  javaguides and from index  4
 last index of given char ::  4

GitHub Repository

Reference


Comments