I want to extract the name of an absolute path. If I have a string with the value /mnt/sdcard/Videos/Videoname, I want to save a string with the value Videoname.
The string is changing and I can't obtain previously the number of slashes. How could I split a substring from the last slash?
/mnt/sdcard/Videos/Videoname --> Videnoname
You should go via the File
api. Quoted from the File.getName()
documentation:
Returns the name of the file or directory denoted by this abstract pathname. This is just the last name in the pathname's name sequence. If the pathname's name sequence is empty, then the empty string is returned.
Example:
String name = new File("/mnt/sdcard/Videos/Videoname").getName();
If you got if from a "File" object, you can get it with the method:
String fileName = myFile.getName();
If you got it from a simple String, you can use
String fileName = myString.substring(myString.lastIndexOf("/")+1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With