0

the android app can load the absolute image path form the sdcard, which looks like this/storage/sdcard0/DCIM/Camera/20130622_200703.jpg I only need part of the path string like/20130622_200703.jpg So I want to recursively split the absolute path into the one I need. I wrote the code like below

public String splitPath(String path){
    if (path == "") {
        return null;
    }

    if(path.indexOf("/") != -1){
        String subPath = path.substring(path.indexOf("/") + 1);
        //absoultePath = subPath;
        splitPath(subPath);
    } 
    return path;
}

But the problem is when I following the process in Debug mode. the path could be split from /.../.../.../.../... to /... which is correct, but it did not return the path even when path.indexOf("/") equals -1.

Then weird things happened, the recursive loop did not end, it continued to change the /... to /.../.../.../.../... step by step, after that process, it returned the path, which was exactly the same value like /storage/sdcard0/DCIM/Camera/20130622_200703.jpg.

I really confused about that, it feels like the logic itself playing joke on me. Oops

Any feedback from you will be appreciated.

2
  • How about "/" + new File(path).getName();? Wouldnt that solve the problem? Commented Aug 21, 2014 at 6:52
  • To keep SO clean and tidy, consider accepting an answer. Commented Aug 21, 2014 at 20:35

3 Answers 3

3

You can easily use split() like this :

public static void main(String a[]) {
    String s = "/storage/sdcard0/DCIM/Camera/20130622_200703.jpg";
    String[] arr = s.split("/");
    System.out.println(arr[arr.length - 1]);
}

O/P:

20130622_200703.jpg
Sign up to request clarification or add additional context in comments.

2 Comments

I wish I could have thought about that idea at the beginning! that saves me
@LoganGuo - You will.. The next time around :)
1

Many, many methods to do this, including a rewritten version of your own code

// making use of lastIndexOf
String subPath = path.substring(path.lastIndexOf("/"));

// make use of file class
String subPath = new File(path).getName();

// make use of String split
String[] parts = path.split("/");
String subPath = parts[parts.length - 1];

// modification on your own code
public static String splitPath(String path) {
    int index = path.indexOf("/");
    if (index == -1)
        return path;
    else 
        return splitPath(path.substring(index + 1));
}

Considerations on your code

public String splitPath(String path){
    if (path == "") { 
        return null;
    }

    if(path.indexOf("/") != -1){
        String subPath = path.substring(path.indexOf("/") + 1);
        splitPath(subPath);
    } 
    return path;
}

you == on a String. Never do that. Suppose you call splitPath("a/b"), then the first if is skipped, the second if is not, but in the second if, you do something with a subString, but never return it, and eventually simply return path anyway. However, a simple return wouldve sufficed:

    if(path.indexOf("/") != -1){
        String subPath = path.substring(path.indexOf("/") + 1);
        return splitPath(subPath); //note the return.
    } 
    return path;

Which results in:

public String splitPath(String path) {
    int index = path == null ? -1 : path.indexOf("/"); // null check
    if (index != -1) 
        return splitPath(path.substring(index + 1));
    return path;
    //or : 
    // int index = path == null ? -1 : path.indexOf("/"); // null check
    // return index == -1 ? path : splitPath(path.subString(index + 1));
}

1 Comment

I want to vote up this commit million times if I could. Very clear answers and inspired me a lot.
1

Use

String subPath = path.substring(path.lastIndexOf("/"));

instead as there is no reason to split it from the beginning.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.