But the search for a cleaner approach that is common. Perhaps for any N.
The call String.lastIndexOf(int,int)in the loop will be quite efficient and possibly quite clean:
int pos = str.length();
for (int i = 0; i < n; i++) {
pos = str.lastIndexOf('/', pos - 1);
}
String out = str.substring(pos + 1);
It can be easily converted into an auxiliary function that takes str, '/'and n, and returns out.