Skip to content

Instantly share code, notes, and snippets.

@sidward35
Created January 7, 2020 10:06
Show Gist options
  • Save sidward35/d49a463476b63fcd751bd7a45e812c13 to your computer and use it in GitHub Desktop.
Save sidward35/d49a463476b63fcd751bd7a45e812c13 to your computer and use it in GitHub Desktop.
Get index of nth occurrence of substring from String in Java
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