How to split filesystem path in Java?

java, path, string

Solution

You can do:

File theFile = new File("/sdcard/images/mr.32.png");
String parent = theFile.getParent();

Or (less recommended)

String path = "/sdcard/images/mr.32.png";
String parent = path.replaceAll("^(.*)/.*?$","$1");

See it

Problem

If I have a string variable inside one class ``` MainActivity.selectedFilePath ``` which has a value like this ``` /sdcard/images/mr.32.png ``` and I want to print somewhere only the path up to that folder without the filename ``` /sdcard/images/ ```

Original source

Related problems