Skip to toolbar

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:




2423total visits,2visits today

Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 2
  • Page views today : 3
  • Total visitors : 48,110
  • Total page view: 66,245

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.