Basic Java – 14 || Difference between IndexOf() and lastIndexOf() methods of Strings in JAVA.

Both indexOf() and lastIndexOf() are methods of String object. These methods will be used to get the position or index of a particular character/substring within the target string.

Important Notes:
  1. Index of a string would start from 0.
  2. The searching of the character or substring is case sensitive. Means small a and capital A are considered differently.
  3. When target string or character is not found in the target String. It will always return -1.
  4. The only difference between these methods in only the way of traversing.



indexOf()

This method returns the first occurrence of the specified character or substring. The traversing would be from starting of the string.

  • indexOf(char ch);
  • indexOf(String str);
  • indexOf(char ch,int fromIndex);
  • indexOf(int fromIndex, char ch);
String temp="Strings in Java. Various methods in Java.";
System.out.println("Position of char 'i': "+temp.indexOf('i'));

Output would be 3 as the first letter is appearing at index 3 in the given string.
System.out.println("Position of char 'i' after given index value: "+temp.indexOf('i',5));

Output would be 8 as traversing is getting started after index 5. And the first instance of i is present at index position 8.

lastIndexOf()

This method returns the last occurrence of the specified character or substring. The traversing would be from starting of the string.

  • indexOf(char ch);
  • indexOf(String str);
String temp="Strings in Java. Various methods in Java.";
System.out.println("Position of char 'i' from end: "+temp.lastIndexOf('i'));

Output would be 33 as last letter is appearing at index 33 in the given string.

For below methods backward traversing works along with the specified index.

  • indexOf(char ch,int fromIndex);
  • indexOf(int fromIndex, char ch);
System.out.println("Position of char 'i' after given index value: "+temp.lastIndexOf('i',5));

Output would be 3 as traversing is getting started after index 5 but from backward in the string.

Code Example

/**
 * 
 */

/**
 * @author ashok.kumar
 *
 */
public class StrIndex {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String temp="Strings in Java. Various methods in Java.";
		System.out.println("Target String: "+temp);
		
		System.out.println("\nExample of indexOf():-");
		System.out.println("Position of char 'i': "+temp.indexOf('i'));
		System.out.println("Position of string 'Java': "+temp.indexOf("Java"));
		System.out.println("Position of string 'java': "+temp.indexOf("java"));	
		System.out.println("Position of char 'i' after given index value: "+temp.indexOf('i',5));
		System.out.println("Position of string 'in' after given index value: "+temp.indexOf("in",13));
		
		System.out.println("\nExample of lastIndexOf():-");
		System.out.println("Position of char 'i' from end: "+temp.lastIndexOf('i'));
		System.out.println("Position of string 'Java': "+temp.lastIndexOf("Java"));
		System.out.println("Position of string 'java': "+temp.lastIndexOf("java"));
		System.out.println("Position of char 'i' after given index value: "+temp.lastIndexOf('i',5));
		System.out.println("Position of string 'in' after given index value: "+temp.lastIndexOf("in",13));
	}

}

Output:
Target String: Strings in Java. Various methods in Java.

Example of indexOf():-
Position of char 'i': 3
Position of string 'Java': 11
Position of string 'java': -1
Position of char 'i' after given index value: 8
Position of string 'in' after given index value: 33

Example of lastIndexOf():-
Position of char 'i' from end: 33
Position of string 'Java': 36
Position of string 'java': -1
Position of char 'i' after given index value: 3
Position of string 'in' after given index value: 8

Related Links:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:




710total visits,1visits today

Leave a Reply

Your email address will not be published. Required fields are marked *