Answers for "java indexof second occurrence"

1

how to get index of second occurrence java

String str = "itiswhatitis";
//returns the index of the first i in "is"
str.indexOf("is");
//returns the index of the second occurence of  i in "is" after the first 
str.indexOf("is", str.indexOf("is") + 1);
Posted by: Guest on October-27-2020
0

java indexof nth occurrence

public static 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;
}
Posted by: Guest on November-04-2020

Code answers related to "java indexof second occurrence"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language