2

I have following string:

String path="/folder1/folder2/folder3/";

I want to remove the first path of the String, so that it becomes:

String path="/folder2/folder3/";

So, what I basically need is to know the index of second backslash and I can use subString method then.

In order to know the 2nd position of /, I can run a for loop and check every character.

But I think there must be a better solution. I want to improve my code knowledge. Any help will be highly appreciated.

Tarek
  • 771
  • 7
  • 18

9 Answers9

2

You can accomplish this using a regular expression replacement:

String newPath = path.replaceAll("^/[^/]*", "");

This means: "after the / at the start of the string (^/), match all of the characters up to but not including the next / ([^/]*);".

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
2

You could use path.indexOf('/', 1) to find the index. This would just skip the first character, which could be a / but it would also make sure that, should the first character not be a / it would still remove the first part of the path.

path = path.substring(path.indexOf('/', 1));

There are other answers using Regex, maybe its because im not good with those, but personally I think in such simple cases, its better to avoid using them, to improve readability.

Subler
  • 657
  • 6
  • 11
  • Thanks, this one simply solved my problem. I also appreciate the regex solution, but may be for more complex solutions. – Tarek Apr 02 '15 at 10:55
1

There are various ways to do that. If you know that there must be a second "/" that implies: there is a first one at index 0. So you could use String.indexOf(char, index) - this version of indexOf searches for the first occurrence of the given character after the provided index. And when you got that second index, you can run subString().

The other option would be to use a simple regular expression to match the second / (see https://docs.oracle.com/javase/tutorial/essential/regex/ for a great introduction to this topic).

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

You can use indexOf() twice:

path.indexOf('/', path.indexOf('/')+1))

will gives you the second slash of the string path.

Jens
  • 67,715
  • 15
  • 98
  • 113
1

The simplest soultion to find second index would be using indexof twice. (You may have to check for -1)

String path="/folder1/folder2/folder3/";
int secondIndex = path.indexOf("/", path.indexOf("/") + 1);
System.out.println(path.substring(secondIndex));

If you can use apache commons then StringUtils have a method ordinalIndexOf that does this.

Syam S
  • 8,421
  • 1
  • 26
  • 36
1

try this

String path="/folder1/folder2/folder3/";
path = path.replaceAll("^/[^/]*", "");
Mark Lee
  • 301
  • 1
  • 2
  • 9
1

if you have a string which is like String path="folder1/folder2/folder3/"; you can simply split it to form and array and use the same to get your desired index.

but in your case I could see an additional "/" before "folder1", this would result in a blank or empty fist cell, thus you would also have to verify for the content, before using the generated array.

Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
1

Try to use java.lang.String.indexOf(String str, int fromIndex) if you are sure about occurrence of the "/" at the 0th index.

    public static void main(String[] args) {

    String path = "/folder1/folder2/folder3/";
    String newPath = path
            .substring(path.indexOf("/", 1), path.length());
    System.out.println(newPath);
}
Rajesh
  • 382
  • 3
  • 14
1

If you have the directories as Path (or File) object you could use the following solution

Path path = Paths.get("/folder1/folder2/folder3/");
Path removedTopDir = path.getRoot().resolve(path.subpath(1, path.getNameCount()));
System.out.println("resolved path: " + removedTopDir);
SubOptimal
  • 22,518
  • 3
  • 53
  • 69