0

I have a string like this (not always the same filename at the end, this is only example)

\\eabman03\edicom\Aterlasning\repstat.txt

And i want to get this

\\eabman03\edicom\Aterlasning\

So I want to strip the filename at the end of this string with Java. How do I do this most efficient way?

3 Answers 3

5
new File(stringValue).getParent()
Sign up to request clarification or add additional context in comments.

2 Comments

much better than string manipulation, because File abstracts the format of the path. so, for instance, you don't have to worry about whether the 'fileSeperator' is "/" or "\\"
Don't you need a .getAbsolutePath()?
0
String str = "\\eabman03\\edicom\\Aterlasning\\repstat.txt";
System.out.println(str.substring(0, str.lastIndexOf('\\')+1));
  1. get the lastindex of \
  2. and use String.subString() method

output:

\eabman03\edicom\Aterlasning\

Comments

0
String dirpath = filepath.replaceAll("(?<=\\\\)[^\\]+", "")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.