Using String.split
method will surely work as told in other answers here.
Also, StringTokenizer
class can be used to to parse the String using /
as the delimiter.
import java.util.StringTokenizer;
public class Test
{
public static void main(String []args)
{
String s = "prefix/dir1/dir2/dir3/dir4/..";
StringTokenizer tokenizer = new StringTokenizer(s, "/");
String dir1 = tokenizer.nextToken();
String dir2 = tokenizer.nextToken();
System.out.println("Dir 1 : "+dir1);
System.out.println("Dir 2 : " + dir2);
}
}
Gives the output as :
Dir 1 : prefix
Dir 2 : dir1
Here you can find more about StringTokenizer.