0

I have a path to a folder and want to change it. But first I want to get a step back.

String path = "C:\Users\Jurgen\Java\Project\Folder\inner_folder\";

How do I get a step back in a path hierarchy? For example:

String path = "C:\Users\Jurgen\Java\Project\Folder\";
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jürgen K.
  • 3,427
  • 9
  • 30
  • 66

2 Answers2

1

Extract a substring up until the last slash

String newPath = path.substring(0, path.lastIndexOf('\'));

Edit: (because I'm being challenged on this answer)

Some people will tell you that treating paths as strings is wrong, in this case it doesn't make a difference. The other option of creating a Path object then using it's .getParent() method or prior to Java 7, a File object.

Ross Drew
  • 8,163
  • 2
  • 41
  • 53
  • 1
    unexplained downvoters, *rolls eyes* – Ross Drew Nov 03 '15 at 14:33
  • Substring operations on Paths are a seriously bad idea *rolls eyes* – Vogel612 Nov 03 '15 at 14:34
  • 3
    You're treating a `Path` as if it were a `String`, which it isn't. – Kayaman Nov 03 '15 at 14:35
  • `path` in this case, is a `String` – Ross Drew Nov 03 '15 at 14:36
  • Capitalized it. Are you happy? Just because it looks like a String and you can in many cases treat it as a `String` does not mean that it is a `String` or you should try to treat it as one. It's a bit like 123 may look like an `int`, but it's actually 3 `chars`. – Kayaman Nov 03 '15 at 14:36
  • 1
    It doesn't look like a `String`, it *is* a `String`. The other answer actually requires it to be "converted" to a `Path` – Ross Drew Nov 03 '15 at 14:39
  • 1
    Yes, the `path` variable in this case is unfortunately a `String`. The sane choice is to convert it to its proper type which is `Path`, instead of trying to treat it as a String which it isn't. It's like asking how could I divide `String number = "100";` by 10? The answer is not "by removing the last character". – Kayaman Nov 03 '15 at 14:42
  • Until java 1.7 there was no path, so back then it wasn't a bad idea to use it as a String – Jürgen K. Nov 03 '15 at 14:44
  • 2
    No, before Java 7 you would have converted it to a `File`. – Kayaman Nov 03 '15 at 14:45
  • 1
    The answer isn't "*by removing the last character*", it's by removing the first character to the left of the decimal point (which is actually quicker than a cast then a division, depending on your needs) and this is nothing like editing the `String` representation of a path. A REST URI is more of a comparison and it's common to do it this way with URIs...because they represent directory structures. Just because there is now an object to do the work for us, doesn't mean it's wrong to do it by hand. – Ross Drew Nov 03 '15 at 14:48
  • 1
    Well, both answers would be a solution. Thanks for that. I have liked both. But this one is more helpfull in my case. – Jürgen K. Nov 03 '15 at 14:55
  • 1
    I've edited it to include your recommendations, people. – Ross Drew Nov 03 '15 at 14:56
  • I'm not challenging you because of me being an annoying b..person. See [here](http://thedailywtf.com/articles/shadow-over-xml) for a heartwarning example what happens when people treat things as `Strings` just because to an uneducated observer it looks like a `String`. – Kayaman Nov 03 '15 at 15:11
  • Never said you were. This is a pretty simple case though. Also, did you post the wrong link? – Ross Drew Nov 03 '15 at 15:43
1

Convert it to a Path with Paths.get(path); and use its getParent() method.

Kayaman
  • 72,141
  • 5
  • 83
  • 121