We are given a string, say, "itiswhatitis" and a substring, say, "is". I need to find the index of 'i' when the string "is" occurs a second time in the original string.
String.indexOf("is") will return 2 in this case. I want the output to be 10 in this case.
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 .
You have to use code like this. int index = s. IndexOf(',', s. IndexOf(',') + 1);
Use overloaded version of indexOf(), which takes the starting index  (fromIndex) as 2nd parameter:
str.indexOf("is", str.indexOf("is") + 1); 
                        I am using: Apache Commons Lang: StringUtils.ordinalIndexOf()
StringUtils.ordinalIndexOf("Java Language", "a", 2) 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With