In this Java tutorial, you will learn how to get the index of nth occurrence of a substring in a string using String.indexOf() function, with examples

Get the index of the nth occurrence of a substring in a String using Java

To find the index of nth occurrence of a substring in a string you can use String.indexOf() function.

A string, say str2, can occur in another string, say str1, n number of times. There could be a requirement in your Java application, that you have to find the position of the nth occurrence of str2 in str1. Be it first occurrence, second or some fifth occurrence.

In this tutorial, we shall learn to get the index of nth occurrence of a string in another string using Java. We shall look into examples, where we consider the case and not consider the case of alphabets in the strings.

We shall make use of String.indexOf(String otherString) method, and a for loop to find the index of nth occurrence of string str2 in str1.

Examples

1. Get index of Nth occurrence of a search string in a string

In this example, we shall initialize two strings with variable names str1 and str2. And we are going to find the second occurrence of str2 in str1. You can change the value of n and observe the result.

Example.java

</>
Copy
public class Example {
	public static void main(String[] args) {
		String str1 = "helloworld good morning good evening good night";
		String str2 = "ing";
		int n = 2;

		int index = nthOccurrence(str1, str2, n);
		System.out.println("index of str2 in str1 at occurrence "+ n +" = "+ index);
	}

	public static int nthOccurrence(String str1, String str2, int n) {

		String tempStr = str1;
		int tempIndex = -1;
		int finalIndex = 0;
		for(int occurrence = 0; occurrence < n ; ++occurrence){
			tempIndex = tempStr.indexOf(str2);
			if(tempIndex==-1){
				finalIndex = 0;
				break;
			}
			tempStr = tempStr.substring(++tempIndex);
			finalIndex+=tempIndex;
		}
		return --finalIndex;
	}
}

When the above program is run, the output to the console is as shown below.

Output

index of str2 in str1 at occurrence 2 = 33

2. Get index of Nth occurrence of a substring ignoring case

In this example, we ignore the case of both the strings and try to find the second occurrence of string str2 in string str1. To ignore the case, we have actually converted the strings to lowercase.

Nore that for the function, nthOccurrence(), we have added a forth argument deciding whether to consider or not consider the case.

Example.java

</>
Copy
public class Example {
	public static void main(String[] args) {
		String str1 = "helloworld good morning good evening good night";
		String str2 = "GOOD";
		int n = 2;

		int index = nthOccurrence(str1, str2, n, true);
		System.out.println("index of str2 in str1 at occurrence "+ n +" = "+ index);
	}

	public static int nthOccurrence(String str1, String str2, int n, boolean ignoreCase) {	
		if(ignoreCase) {
			str1 = str1.toLowerCase();
			str2 = str2.toLowerCase();
		}
		
		String tempStr = str1;
		int tempIndex = -1;
		int finalIndex = 0;
		for(int occurrence = 0; occurrence < n ; ++occurrence){
			tempIndex = tempStr.indexOf(str2);
			if(tempIndex==-1){
				finalIndex = 0;
				break;
			}
			tempStr = tempStr.substring(++tempIndex);
			finalIndex+=tempIndex;
		}
		return --finalIndex;
	}
}

Output

index of str2 in str1 at occurrence 2 = 24

Conclusion

In this Java Tutorial, we have learned how to get index of nth occurrence of a substring in a String, by considering or ignoring the case of alphabets.