Back
String variable contains a file name, C:\Hello\AnotherFolder\The File Name.PDF. How do I only get the file name The File Name.PDF as a String?
String
C:\Hello\AnotherFolder\The File Name.PDF
The File Name.PDF
I planned to split the string, but that is not the optimal solution.
just use File.getName()
File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF");System.out.println(f.getName());
File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF");
System.out.println(f.getName());
using String methods:
File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF"); System.out.println(f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf("\\")+1));
System.out.println(f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf("\\")+1));
31k questions
32.8k answers
501 comments
693 users