簡體   English   中英

在java中找到字符串中子串的第n個出現位置?

[英]find the nth occurence of a substring in a string in java?

我有一個字符串,它是html頁面的完整內容,我試圖找到</table>的第二次出現的索引。 有沒有人對如何實現這一點有任何建議?

使用indexOf概括了@BasVanDenBroek的答案

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;
}

快速而骯臟的測試:

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));
}

這是一個有趣的鏡頭;)

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

首先找到第一個索引,然后查找從第一個索引+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);

這是一些非常基本的代碼btw,你會想要建立一些額外的檢查(索引!= -1等)同樣在你的帖子標題中它說nth出現,但在你的帖子中你提到第二次出現具體。 如果你確實需要第n次出現,我相信你能從這里弄明白。

https://stackoverflow.com/a/5678546/15789https://stackoverflow.com/a/14356988/15789上進一步工作(感謝@ sebastiaan-van-den-broek和@assylias的原創海報)。

獲取數組中的所有索引。 然后你可以獲得任何第n個索引。 在許多情況下,可能需要多次獲取字符串中子字符串的第n個索引。 獲取一次數組並多次訪問它可能會更容易。

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM