Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse this string in Java?

Tags:

java

string

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

How to parse the dir1, dir2 values out of the above string in Java?

The prefix here can be:

/usr/local/apache2/resumes

like image 559
omg Avatar asked Jun 04 '09 12:06

omg


People also ask

What is parse in Java?

Parsing is to read the value of one object to convert it to another type. For example you may have a string with a value of "10". Internally that string contains the Unicode characters '1' and '0' not the actual number 10. The method Integer. parseInt takes that string value and returns a real number.

Can you parse characters in Java?

Strings in Java can be parsed using the split method of the String class. ( StringTokenizer can also be used to parse a string; we won't be covering it here).

What is Parsable in Java?

A Parcelable is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.


6 Answers

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.

like image 111
coobird Avatar answered Oct 24 '22 21:10

coobird


...
String str = "bla!/bla/bla/"

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

//To get fist "bla!"
String dir1 = parts[0];
like image 24
user101375 Avatar answered Oct 24 '22 21:10

user101375


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

like image 30
n3rd Avatar answered Oct 24 '22 22:10

n3rd


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
}
like image 24
Edward Dale Avatar answered Oct 24 '22 20:10

Edward Dale


 String result;
 String str = "/usr/local/apache2/resumes/dir1/dir2/dir3/dir4";
 String regex ="(dir)+[\\d]";
 Matcher matcher = Pattern.compile( regex ).matcher( str);
  while (matcher.find( ))
  {
  result = matcher.group();     
  System.out.println(result);                 
}

output-- dir1 dir2 dir3 dir4

like image 41
Raghunandan Avatar answered Oct 24 '22 22:10

Raghunandan


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.

like image 30
Amit Upadhyay Avatar answered Oct 24 '22 20:10

Amit Upadhyay