• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

string processing

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Thomas Murphy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic