我有一个字符串,它是 html 页面的完整内容,我试图找到第二次出现的索引</table>
。有人对如何实现这一目标有任何建议吗?
这是一个有趣的镜头;)
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)