permaculture playing cards*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string processing" Watch "string processing" New topic
Author

string processing

Thomas Murphy
Greenhorn

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:

C:/root/user/home/source/foo.java

i want to extract:

C:/root/user

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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 java.io.File 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.io.File.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
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.

http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html

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
Sheriff

Joined: Dec 10, 2001
Posts: 7023
The API documentation Jesper referred to is available at http://java.sun.com/api. 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
Greenhorn

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.
 
wood burning stoves
 
subject: string processing
 
Similar Threads
Problems reading file
making a jar file of images
expand the ./filename to fully qualified path automatically
Problems reading file
replacing string with '\' character