Skip to content Skip to sidebar Skip to footer

How To Split A File Path To Get The File Name?

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 l

Solution 1:

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

Solution 2:

one another possibility:

String lStr = "/storage/emulated/0/temp.jpg";
lStr = lStr.substring(lStr.lastIndexOf("/")+1);
System.out.println(lStr);

Solution 3:

You can do it with string split: How to split a string in Java

Stringstring = "/storage/emulated/0/temp.jpg";
String[] parts = string.split("/");
String file= parts[parts.length-1]; 

Solution 4:

Try this:

String path= "/storage/emulated/0/temp.jpg";
String[] parts = path.split("/");
String filename;
if(parts.length>0)
    filename= parts[parts.length-1];  

Solution 5:

Stringstring = "/storage/emulated/0/temp.jpg";
String[] splitString = null;
splitString = string.split("/");
splitString[splitString.length - 1];//this is where your string will be

Try using the String function split. It splits the string by your input and returns an array of strings. Just access the last element of the array in your case.

Post a Comment for "How To Split A File Path To Get The File Name?"