How can we split super Java String to sub String alone?

时间:2017-08-05 10:35:56

标签: java string

eg:

superpath = /path/to/a/b/c/d

How can we split superpath to

subpath1 = /path/to/a

subpath2 = /path/to/a/b

subpath3 = /path/to/a/b/c

subpath = /path/to/a/b/c/d

sorry for not clarity about this question.

What's my question is:is there one method to do this?,do not split、and append again. like below:

String superpath = "/path/to/a/b/c/d" String [] subStrList = superPath.someMethod(somePar); //subStrList[i] equals "/path/to/a"

I know , I can use the split method to split String step by step, I have searched Google and official Java docs, I can't find a API interface to do this ,so I posted a question here . may be somebody misunderstand my intent。

My

2 个答案:

答案 0 :(得分:1)

Something like this ?

        String str = "/path/to/a/b/c/d";
        String[] strArray= str.split("/");
        String temp = "";
        ArrayList<String> arrayList = new ArrayList<String>();
        for(int i=1; i<strArray.length; i++){
            temp = temp + "/" + strArray[i];
            arrayList.add(temp);
        }

here arrayList consists of all sub paths

答案 1 :(得分:0)

try this :

String[] array = superPath.replace("/path/to/" , "").split('/');
List<String> paths = new ArrayList<>();
for(String item : array)
{
  temp = temp + "/" + item;
  paths.add("/path/to" + temp);  
}