这是一个好玩的镜头;)
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;
}