Removing a part of a string before second /

会有一股神秘感。 提交于 2019-12-13 01:07:57

问题


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.


回答1:


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.




回答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 / ([^/]*);".




回答3:


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).




回答4:


You can use indexOf() twice:

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

will gives you the second slash of the string path.




回答5:


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.




回答6:


try this

String path="/folder1/folder2/folder3/";
path = path.replaceAll("^/[^/]*", "");



回答7:


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.




回答8:


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);
}



回答9:


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);


来源:https://stackoverflow.com/questions/29408839/removing-a-part-of-a-string-before-second

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!