残念ながら、本番環境にJava 7がないため、ソリューションを使用できません。しかし、私は他の解決策を思いついた
public static void main(String[] args){
String source="Old/0123340/d/e/a-ler/busy";
int pos1=nthOccurrence(source,'/',2);
int pos2=nthOccurrenceFromLast(source,'/',1);
System.out.println(source.substring(pos1+1, pos2));
//gives output d/e/a-ler
}
public static int nthOccurrence(String str, char c, int n) {
int pos = str.indexOf(c, 0);
while (--n > 0 && pos != -1)
pos = str.indexOf(c, pos+1);
return pos;
}
public static int nthOccurrenceFromLast(String str, char c, int n) {
int pos = str.lastIndexOf(c, str.length());
while (--n > 0 && pos != -1)
pos = str.lastIndexOf(c, pos-1);
return pos;
}