在java中查找字符串中第n次出现的子字符串?

在java中查找字符串中第n次出现的子字符串?,java,string,substring,Java,String,Substring,我有一个字符串,它是html页面的完整内容,我正在尝试查找第二次出现的的索引。有人对如何实现这一点有什么建议吗?首先找到第一个索引,然后从第一个索引+1开始搜索第二个索引 String string = "first</table>second</table>"; int firstIndex = string.indexOf("</table>"); int secondIndex = string.indexOf("</table>", fir

我有一个字符串,它是html页面的完整内容,我正在尝试查找第二次出现的
的索引。有人对如何实现这一点有什么建议吗?

首先找到第一个索引,然后从第一个索引+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);
String String=“firstsecond”;
int firstIndex=string.indexOf(“”);
int secondIndex=string.indexOf(“,firstIndex+1);
System.out.println(“第二个索引:+secondIndex”);
这是一些非常基本的代码,顺便说一句,您需要构建一些额外的检查(索引!=-1等) 在你的文章标题中也提到了第n次发生,但在你的文章中,你特别提到了第二次发生。如果你真的需要第n次出现,我相信你可以从这里找到答案。

这是一个有趣的镜头;)

public static int findNthIndexOf(字符串str、字符串指针、int出现)
抛出IndexOutOfBoundsException{
int指数=-1;
Pattern p=Pattern.compile(针形、模式、多行);
匹配器m=p.Matcher(str);
while(m.find()){
如果(--occurrence==0){
索引=m.start();
打破
}
}
如果(索引<0)抛出新的IndexOutOfBoundsException();
收益指数;
}

查找第n个字符串的另一个好方法是使用Apache Commons:

StringUtils.ordinalIndexOf("aabaabaa", "b", 2)  == 5
使用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;
}
进一步研究和(感谢原创海报@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;
}
publicstaticint[]getindex(字符串源、字符串子字符串){
列表指示列表=空;
int index=source.indexOf(substr);
如果(索引==-1){
返回新整数[0];
}否则{
indicesList=newarraylist();
索引列表。添加(索引);
}
while(索引!=-1){
index=source.indexOf(substr,index+1);
如果(索引!=-1){
索引列表。添加(索引);
}
}
//整数[]iarr=新整数[1];
//自动装箱不适用于数组。请运行循环进行转换。
//toArray不将整数[]转换为int[]
int[]index=newint[indicesList.size()];
对于(int i=0;i
@Jon:你怎么知道这是家庭作业@蒂乔:是吗?或者你是想在你正在编写的真实程序中做到这一点。只要你说,家庭作业就可以了。我想它和这些差不多;)我的问题有什么问题吗。。?我有一个字符串,它是hhtml页面的完整内容。我想知道第二次出现的“”i的索引…@T.J。我认为大多数编写真实程序的人都能够在这个页面上查看和搜索单词出现。找出哪个单词的出现标志着最合适的功能。@T.J.Crowder:家庭作业…???你的意思是…?是的,我试图在一个实际的项目中实现这一点我正在工作,…在正则表达式中,<一个特殊的字符不是你应该逃避的吗@Bas-不,它在Java的正则表达式中不是一个特殊的字符:+1,因为最好不要重新发明轮子。不是“另一个好的选择”,只是最好/最安全/最快的选择。谢谢。这一个比公认的解决方案好,因为另一个使用模式匹配,因此速度较慢。这个要快两个数量级
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[] 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;
}