-1

I am trying to parse a string in a way that is probably uncommon. I can't find the answer anywhere online. This string involves the directory path of the uploaded picture in the ImageView. Directory string have multiple "/" characters in them. How do I tell the app to look at the last "/" character in the string? Here is an example...

Directory Location of the Uploaded Image in the ImageView...

/storage/emulated/0/My Pictures/My family photo.png

I want to split that string to only show My family photo. The folders and the dot extension I want removed. Only the file name should be displayed.

I've tried this...

String s = getAbsolutePath; //This contains the full string location in my example above.
String[] split1 = s.split("/");
String newS = split1[1];
String[] split2 = newS.split(".png");
String titleString = split2[0];

titleString should now contain this data string My family photo.

But the result output is actually storage

So now, how do I code this to tell the app look at the last / in the string just before the filename?

I appreciate the help! Thanks a bunch!!!

  • split the array on `/`, find the last valid index of your array. last valid index is the size of your array minus 1, so this is probably something like `split[size -1]`. -1 because arrays are zero based, while size will return the amount of items in the array, so one item doesn't mean it's at index 1, it's at 0 – a_local_nobody Mar 19 '21 at 12:35
  • Can't you just create a File object and use `file.getName()` – ADM Mar 19 '21 at 12:42

4 Answers4

2

Simplest way:

    String path = "/storage/emulated/0/My Pictures/My family photo.png";
    System.out.println(Paths.get(path).toFile().getName().replace(".png", ""));

Furthermore I suggest to discover package java.nio.* there is so much usefull tolls if you are dealing with file system.

Ludov Dmitrii
  • 425
  • 5
  • 9
  • 1
    It's a very clean solution, just for the extension removal part I would suggest to consider this post: [How do I trim a file extension from a String in Java?](https://stackoverflow.com/questions/941272/how-do-i-trim-a-file-extension-from-a-string-in-java) – Alex Sveshnikov Mar 19 '21 at 12:44
1

You could do this with regular expressions:

String path = "/storage/emulated/0/My Pictures/My family photo.png";
String filename = path.replaceAll(".*/(.*)\\.png","$1");
Henry
  • 42,982
  • 7
  • 68
  • 84
0

After your first split you get an array with the following elements:

{"", "storage", "emulated", "0", "My Pictures", "My family photo.png"}

So in your 3rd line where you wrote split1[1] you are getting the second element of the above array, which is storage. In your case, you want to get the last element. So change your code to this:

String s = getAbsolutePath; //This contains the full string location in my example above.
String[] split1 = s.split("/");
String newS = split1[split1.length - 1];
String[] split2 = newS.split(".png");
String titleString = split2[0];

split1.length - 1 is the index of the last element in your array, which is My family photo.png

Hidayat Rzayev
  • 339
  • 3
  • 14
0

Result is "My family photo"

  String x = "/storage/emulated/0/My Pictures/My family photo.png";
  
  int index = x.lastIndexOf("/") + 1 ;

  x =  x.substring(index , x.lastIndexOf("."));

  
Rohaitas Tanoli
  • 624
  • 4
  • 16