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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you want to split the
Stringat the/character, theString.splitmethod will work:For example:
Output
Edit
Case with a
/in the prefix, and we know what the prefix is:The substring without the prefix
"slash/prefix/"is made by thesubstringmethod. ThatStringis then run throughsplit.Output:
Edit again
If this
Stringis actually dealing with file paths, using theFileclass is probably more preferable than using string manipulations. Classes likeFilewhich already take into account all the intricacies of dealing with file paths is going to be more robust.