[英]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>
. 我有一个字符串,它是html页面的完整内容,我试图找到</table>
的第二次出现的索引。 Does anyone have any suggestions on how to achieve this? 有没有人对如何实现这一点有任何建议?
A generalization of @BasVanDenBroek's answer , using indexOf: 使用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;
}
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 首先找到第一个索引,然后查找从第一个索引+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. 这是一些非常基本的代码btw,你会想要建立一些额外的检查(索引!= -1等)同样在你的帖子标题中它说nth出现,但在你的帖子中你提到第二次出现具体。 I'm sure you'll be able to figure it out from here if you actually need the nth occurence though. 如果你确实需要第n次出现,我相信你能从这里弄明白。
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). 在https://stackoverflow.com/a/5678546/15789和https://stackoverflow.com/a/14356988/15789上进一步工作(感谢@ sebastiaan-van-den-broek和@assylias的原创海报)。
Get all the indices in an array. 获取数组中的所有索引。 Then you can get any nth index. 然后你可以获得任何第n个索引。 In many cases, it may be required to get the nth index of a substring within a string multiple number of times. 在许多情况下,可能需要多次获取字符串中子字符串的第n个索引。 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.