简体   繁体   中英

find the nth occurence of a substring in a string in java?

I have a string that is the complete content of an html page and I am trying to find the index of 2nd occurence of </table> . Does anyone have any suggestions on how to achieve this?

A generalization of @BasVanDenBroek's answer , using indexOf:

public static int nthIndexOf(String source, String sought, int n) {
    int index = source.indexOf(sought);
    if (index == -1) return -1;

    for (int i = 1; i < n; i++) {
        index = source.indexOf(sought, index + 1);
        if (index == -1) return -1;
    }
    return index;
}

Quick and dirty test:

public static void main(String[] args) throws InterruptedException {
    System.out.println(nthIndexOf("abc abc abc", "abc", 1));
    System.out.println(nthIndexOf("abc abc abc", "abc", 2));
    System.out.println(nthIndexOf("abcabcabc", "abc", 2));
    System.out.println(nthIndexOf("abcabcabc", "abc", 3));
    System.out.println(nthIndexOf("abc abc abc", "abc", 3));
    System.out.println(nthIndexOf("abc abc defasabc", "abc", 3));
    System.out.println(nthIndexOf("abc abc defasabc", "abc", 4));
}

Here is a shot for fun ;)

public static int findNthIndexOf (String str, String needle, int occurence)
            throws IndexOutOfBoundsException {
    int index = -1;
    Pattern p = Pattern.compile(needle, Pattern.MULTILINE);
    Matcher m = p.matcher(str);
    while(m.find()) {
        if (--occurence == 0) {
            index = m.start();
            break;
        }
    }
    if (index < 0) throw new IndexOutOfBoundsException();
    return index;
}

找到第N个String的另一个好方法是使用Apache Commons的StringUtils.ordinalIndexOf()

StringUtils.ordinalIndexOf("aabaabaa", "b", 2)  == 5

First find the first index, then look for the second index starting your search from the first index +1

String string = "first</table>second</table>";
int firstIndex = string.indexOf("</table>");
int secondIndex = string.indexOf("</table>", firstIndex+1);
System.out.println("second index: " + secondIndex);

This is some pretty basic code btw, you will want to build some additional checks (index != -1 and the like) Also in your post title it said nth occurence but in your post you mention the second occurence specifically. I'm sure you'll be able to figure it out from here if you actually need the nth occurence though.

Working further on https://stackoverflow.com/a/5678546/15789 and https://stackoverflow.com/a/14356988/15789 (Thanks to original posters @sebastiaan-van-den-broek and @assylias).

Get all the indices in an array. Then you can get any nth index. In many cases, it may be required to get the nth index of a substring within a string multiple number of times. Getting an array once and accessing it multiple times may be easier.

public static int[] getIndices(String source, String substr) {
    List<Integer> indicesList = null;
    int index = source.indexOf(substr);
    if (index == -1) {
        return new int[0];
    } else {
        indicesList = new ArrayList<>();
        indicesList.add(index);
    }

    while (index != -1) {
        index = source.indexOf(substr, index + 1);
        if (index != -1) {
            indicesList.add(index);
        }
    }

    // Integer[] iarr = new int[1]; 
    //Autoboxing does not work with arrays. Run loop to convert. 
    //toArray does not convert Integer[] to int[]
    int[] indices = new int[indicesList.size()];
    for (int i = 0; i < indicesList.size(); i++) {
        indices[i] = indicesList.get(i);
    }
    return indices;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM