File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes string processing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string processing" Watch "string processing" New topic

string processing

Thomas Murphy

Joined: Dec 14, 2006
Posts: 23
Hi all,

i want to strip out the root path from a file path.
basically i think it would be easiest to to gobble up everything in the string prior to the 3rd instance of a '/'

so if the file path is:


i want to extract:


any hints or alternatives humbly appreciated
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

Have a look at the API documentation for class java.lang.String. It contains a number of methods to do string processing, for example to find characters like '/' in a string.

Since you're dealing with file paths here, you might want to use class for this job instead. It automatically handles the different path delimiter characters that are used on different operating systems (for example, '\' on Windows and '/' on Unix and other OS'es). So, also have a look at the API documentation of class

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
One alternative along the same lines as what you're thinking is to reverse the String and read up to the first "/". This will separate out the file name and the path to the file. Then just reverse the two resulting Strings to get them back into usable form.

The best way is probably to just use Java's File object to do the work for you.

You'll have to experiment a bit with it, but I think you want the File(String pathname) constructor. Then try the getPath() and getAbsolutePath() methods to see what results you get.

I'm not sure exactly what you're looking for or I'd provide an example. It's probably better for you to experiment anyway, since File is central to a lot of programming work in Java.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
The API documentation Jesper referred to is available at This documentation is definitely one of a Java developer's Super Best Friends.

If you're not yet familiar with using regular expressions in Java, this might be a good time to start learning about them. A decent introduction to this subject is in the two "An Introduction to java.util.regex" articles in the JavaRanch Journal.

Way Big Hint ('cuz you got lucky that I was just now writing a regular expression for something else):
[ June 12, 2007: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Thomas Murphy

Joined: Dec 14, 2006
Posts: 23
thanks to everyone who replied here.
it was very beneficial for what i needed and then some.
Dirk i went with your help here as it worked exactly and i was unaware until now of how to use lazy matching effectively.
I agree. Here's the link:
subject: string processing
jQuery in Action, 3rd edition