Logan Guo
Logan Guo

Reputation: 886

android recursively split string exception

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.

Upvotes: 0

Views: 102

Answers (3)

stealthjong
stealthjong

Reputation: 11093

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

Upvotes: 1

Eypros
Eypros

Reputation: 5723

Use

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

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

Upvotes: 1

TheLostMind
TheLostMind

Reputation: 36304

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

Upvotes: 3

Related Questions