Menu
Topics Index
...
`


Strings >
Siva Nookala - 01 Apr 2016
The String class provides 2 methods for searching a string. They are :
  • indexOf() : Searches for the first occurrence of a character or substring.
  • lastIndexOf() : Searches for the last occurrence of a character or substring.
These methods return the starting index of character or a substring on a successful search else they return -1.

There are many overloaded forms of these methods:
Syntax Explanation
int indexOf(char ch)
This method will return the index of the first occurrence of a character variable ch in the invoked string.
int lastIndexOf(char ch)
This method will return the index of the last occurrence of a character variable ch in the invoked string.
int indexOf(String st)
This method will return the index of the first occurrence of a substring st in the invoked string.
int lastIndexOf(String st)
This method will return the index of the last occurrence of a substring st in the invoked string.
int indexOf(char ch, int startIndex)
int indexOf(String st, int startIndex)
Here startIndex specifies the starting point of search. The search runs from startIndexto end of the String.
int lastIndexOf(char ch, int startIndex)
int lastIndexOf(String st, int startIndex)
Here startIndex specifies the starting point of search. The search runs from startIndex to zero.


This example program elaborates these methods.
SearchingStrings
class StringSearchDemo
{
    public static void main(String arg[])
    {
        String str = "The Sun rises in the east and sets in the west.";
        System.out.println("Length of str : " + str.length());
        System.out.println("indexOf(i) : " + str.indexOf('i')); // LINE A
        System.out.println("lastIndexOf(i) : " + str.lastIndexOf('i')); // LINE B
        System.out.println("indexOf(st) : " + str.indexOf("st")); // LINE C
        System.out.println("lastIndexOf(st) : " + str.lastIndexOf("st")); // LINE D
        System.out.println("indexOf(e, 2) : " + str.indexOf('e', 2)); // LINE E
        System.out.println("lastIndexOf(e, 46) : " + str.lastIndexOf('e', 46)); // LINE F
        System.out.println("indexOf(st, 2) : " + str.indexOf("st", 2)); // LINE G
        System.out.println("lastIndexOf(st, 46) : " + str.lastIndexOf("st", 46)); // LINE H    
    }
}
OUTPUT

Length of str : 47
indexOf(i) : 9
lastIndexOf(i) : 35
indexOf(st) : 23
lastIndexOf(st) : 44
indexOf(e, 2) : 2
lastIndexOf(e, 46) : 43
indexOf(st, 2) : 23
lastIndexOf(st, 46) : 44

DESCRIPTION

LINE A is the display of index of first occurrence of character 'i'. LINE B is the display of index of last occurrence of character 'i'. LINE C is the display of index of first occurrence of substring "st". LINE Dis the display of index of last occurrence of substring "st".

LINE E is the display of index of first occurrence of character 'e' while the search starts at index 2 and continues till the end of String str. LINE F is the display of index of first occurrence of character 'e' while the search runs from index 46 to zero of String str.

LINE G is the display of index of first occurrence of substring "st" while the search starts at index 2 and continues till the end of String str. LINE H is the display of index of first occurrence of substring "st" while the search runs from index 46 to zero of String str.

THINGS TO TRY
  • Observe the output for this code.
    String str = "The Sun rises in the east and sets in the west.";
    System.out.println("indexOf(c) : " + str.indexOf('c'));
    The output for the above code : indexOf(c) -- -1
    If the passed argument character is not found in the string indexOf method returns -1.
  • Try this code
    String str = "The Sun rises in the east and sets in the west.";
    System.out.println("lastIndexOf(st, 0) : " + str.lastIndexOf("st", 0));
    System.out.println("indexOf(a) : " + str.indexOf(97));
    The output for the above code : lastIndexOf(st, 0) : -1
    indexOf(a) : 22
    lastIndexOf method searchs for the last occurence of the argument string from the passed argument index to the starting index i.e. 0, but starting index is passed as zero, so the output will become -1. for the lastIndexOf(st, 0) and indexOf(a) will return the first occurence index value of a in the string.

Dependent Topics : Java String trim() Method - trim() Method In Java  Additional String Methods in Java  Java setLength() Method In StringBuffer Class  Java delete() and deleteCharAt() Methods In StringBuffer 

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App