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
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.
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).
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.
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.
...
String str = "bla!/bla/bla/"
String parts[] = str.split("/");
//To get fist "bla!"
String dir1 = parts[0];
In this case, why not use new File("prefix/dir1/dir2/dir3/dir4")
and go from there?
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 }
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With