Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a file path to get the file name? [duplicate]

I have this string in my Android Application:

/storage/emulated/0/temp.jpg

I need manipulate the string and to split the string for this output:

temp.jpg

I need always take the last element of the string.

How to this output in java?

I would greatly appreciate any help you can give me in working this problem.

like image 758
Antonio Mailtraq Avatar asked Sep 24 '14 14:09

Antonio Mailtraq


People also ask

How do I separate file names in Excel?

Split Names tool - fastest way to separate names in ExcelSelect any cell containing a name you want to separate and click the Split Names icon on the Ablebits Data tab > Text group. Select the desired names parts (all of them in our case) at click Split.

What is split path?

The Split-Path cmdlet returns only the specified part of a path, such as the parent folder, a subfolder, or a file name. It can also get items that are referenced by the split path and tell whether the path is relative or absolute. You can use this cmdlet to get or submit only a selected part of a path.


2 Answers

This is not a string splitting exercise

If you need to get a file name from a file path, use the File class:

File f = new File("/storage/emulated/0/temp.jpg");
System.out.println(f.getName());

Output:

temp.jpg
like image 158
Duncan Jones Avatar answered Oct 02 '22 02:10

Duncan Jones


one another possibility:

String lStr = "/storage/emulated/0/temp.jpg";
lStr = lStr.substring(lStr.lastIndexOf("/")+1);
System.out.println(lStr);
like image 23
s_bei Avatar answered Oct 02 '22 02:10

s_bei