tags:

views:

5292

answers:

8

"prefix/dir1/dir2/dir3/dir4/.."

how to parse the dir1,dir2 .. value out of the above string in JAVA?

The prefix here can be : /usr/local/apache2/resumes

+13  A: 

If you want to split the String at the / character, the String.split method will work:

For example:

String s = "prefix/dir1/dir2/dir3/dir4";
String[] tokens = s.split("/");

for (String t : tokens)
  System.out.println(t);

Output

prefix
dir1
dir2
dir3
dir4

Edit

Case with a / in the prefix, and we know what the prefix is:

String s = "slash/prefix/dir1/dir2/dir3/dir4";

String prefix = "slash/prefix/";
String noPrefixStr = s.substring(s.indexOf(prefix) + prefix.length());

String[] tokens = noPrefixStr.split("/");

for (String t : tokens)
  System.out.println(t);

The substring without the prefix "slash/prefix/" is made by the substring method. That String is then run through split.

Output:

dir1
dir2
dir3
dir4

Edit again

If this String is actually dealing with file paths, using the File class is probably more preferable than using string manipulations. Classes like File which already take into account all the intricacies of dealing with file paths is going to be more robust.

coobird
What if there is / character in prefix?
Shore
+1 Beat me by 15s
basszero
Shore, you should edit your question and clarify the possible values of prefix, dir1, etc, etc ... Are you tring to parse a URL?
basszero
Yes,absolutely,I've updated my post.
Shore
What if there's a / postfix?
Apocalisp
A: 
String s = "prefix/dir1/dir2/dir3/dir4"

String parts[] = s.split("/");

System.out.println(s[0]); // "prefix"
System.out.println(s[1]); // "dir1"
...
basszero
There can have / in prefix too.
Shore
+2  A: 

In this case, why not use new File("prefix/dir1/dir2/dir3/dir4") and go from there?

n3rd
A: 
public class Test {
    public static void main(String args[]) {
    String s = "pre/fix/dir1/dir2/dir3/dir4/..";
    String prefix = "pre/fix";
    String[] tokens = s.substring(prefix.length()).split("/");
    for (int i=0; i<tokens.length; i++) {
     System.out.println(tokens[i]);
    }
    }

}
Ken
There can have / in prefix too.prefix here is not literally 'prefix'
Shore
Do you know the prefix going into the parse? Then get the length of the prefix and use substring to before calling parse to just get the last segment of the string.
Ken
A: 
...
String str = "bla!/bla/bla/"

String parts[] = s.split("/");

//To get fist "bla!"
String dir1 = splitStr[0];
Sorry, my brain can't compile this! And it was even edited...
Ilia K.
A: 

If it's a File, you can get the parts by creating an instanceof File and then ask for its segments.

This is good because it'll work regardless of the direction of the slashes; it's platform independent (except for the "drive letters" in windows...)

Scott Stanchfield
A: 
String str = "/usr/local/apache/resumes/dir1/dir2";
String prefix = "/usr/local/apache/resumes/";

if( str.startsWith(prefix) ) {
  str = str.substring(0, prefix.length);
  String parts[] = str.split("/");
  // dir1=parts[0];
  // dir2=parts[1];
} else {
  // It doesn't start with your prefix
}
scompt.com
Almost - you are just parsing the prefix with that substr. Try str = str.substring(prefix.length) instead.
Ken
A: 

String.split(String regex) is convenient but if you don't need the regular expression handling then go with the substring(..) example, java.util.StringTokenizer or use Apache commons lang [1]. The performance difference when not using regular expressions can be a gain of 1 to 2 orders of magnitude in speed.