Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain name from absolute path, substring from last slash, java android

Tags:

java

file

android

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

like image 495
Álvaro Avatar asked May 11 '12 10:05

Álvaro


2 Answers

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();
like image 197
dacwe Avatar answered Oct 27 '22 00:10

dacwe


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);
like image 29
Jeje Doudou Avatar answered Oct 26 '22 23:10

Jeje Doudou