How to remove the last slash and a string from a string in Java?

Hi,
i have the following type of string and I would like to trim it. Can you please help me with the regexp?

Input
 S:\TestDir\Test_File.txt

Open in new window


Expected Output
 S:\TestDir\

Open in new window


The general rule is remove everything after the last slash.

How can I write this regexp in Java?
TolgarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brendanmeyerCommented:
String ResultString = null;

Pattern regex = Pattern.compile("^.*\\\\", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
	ResultString = regexMatcher.group();

Open in new window

0
Terry WoodsIT GuruCommented:
Do a regex replace with pattern:
"(?<=\\\\)[^\\\\]*$"
replacing with an empty string.
0
TolgarAuthor Commented:
Oh btw, I forgot to say. The slashes can be either forward or backward.

How can I cover it in one regexp?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Terry WoodsIT GuruCommented:
Try this:

mystring = mystring.replaceAll("(?<=[\\\\/]/)[^\\\\/]*$", "");

Open in new window


It will give you trouble if you have a slash in your file name though.
0
brendanmeyerCommented:
"^.*[\/]"
0
Michael FowlerSolutions ConsultantCommented:
Instead of using a regex you could always use LastIndexOf

File file = File("S:\\TestDir\\Test_File.txt");
String absolutePath = file.getAbsolutePath();
String filePath = absolutePath.substring(0,absolutePath.lastIndexOf(File.separator));

Michael
0
TolgarAuthor Commented:
Sorry! I accpeted the solution but they both do not work.

I wonder if you make an assumption that there will always be 2 slashes in the whole string.

If so, it is not right.

The regexp should also cover this case:

S:\TestDir\TestDir2\Test_File.txt

Open in new window


expected result is:
S:\TestDir\TestDir2\

Open in new window



or this case


S:\TestDir\TestDir2\TestDir3\Test_File.txt

Open in new window


expected result is:

S:\TestDir\TestDir2\TestDir3\

Open in new window


or this case:

S:\TestDir\TestDir2\TestDir3\TestDir4\Test_File.txt

Open in new window


expected result is:

S:\TestDir\TestDir2\TestDir3\TestDir4\

Open in new window


or any level of directory structure.


How can I do it in one regexp? (The slashes can be forward or backward)
0
TolgarAuthor Commented:
@Michael: You are right but the problem is. I have String and I cannot cast it to a File.

Do you have any suggestion?
0
Terry WoodsIT GuruCommented:
Sorry, I found a typo in my solution. It should be:
mystring = mystring.replaceAll("(?<=[\\\\/])[^\\\\/]*$", "");

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brendanmeyerCommented:
String ResultString = null;
Pattern regex = Pattern.compile("^.*[\\\\/]", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
	ResultString = regexMatcher.group();
} 

Open in new window

0
Terry WoodsIT GuruCommented:
A moderator will be happy to reopen the question if you want to reassign points. Just make the request through the Request Attention link just below your question.
0
TolgarAuthor Commented:
This is the original code with Michael's suggestion:

            FileDialog dlg = new FileDialog(shell);
            String fileListFile = dlg.open();

                       
            String absolutePath = fileListFile.getAbsolutePath();
            String fileListPath = absolutePath.substring(0,absolutePath.lastIndexOf(File.separator));
            
            if (fileListFile!=null && !fileListFile.equals("")){
                Gui.readTxt(fileListFile, fileListPath);
            }

Open in new window

0
Michael FowlerSolutions ConsultantCommented:
The dialog will return the absolute path as a string and so you can just use
FileDialog dlg = new FileDialog(shell);
String fileListFile = dlg.open();
String fileListPath = fileListFile.substring(0,fileListFile.lastIndexOf(File.separator));        

Open in new window

Michael
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.