Created
January 7, 2020 10:06
-
-
Save sidward35/d49a463476b63fcd751bd7a45e812c13 to your computer and use it in GitHub Desktop.
Get index of nth occurrence of substring from String in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int ordinalIndexOf(String str, String substr, int n) { | |
int pos = str.indexOf(substr); | |
while (--n > 0 && pos != -1) | |
pos = str.indexOf(substr, pos + 1); | |
return pos; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment