I have a filepath like filepath = "Y:\abc\hello\doc.txt". I want to get substring from "\doc.txt". How can I do it using Java code.
Asked
Active
Viewed 27 times
0
-
use the split method of the String class, split on \ and keep "\" + last result. – Stultuske May 31 '22 at 09:05
-
Why do you want `\doc.txt` instead of `doc.text`? – Mark Rotteveel May 31 '22 at 09:17
-
Have a look at [`Path#getFileName()`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/Path.html#getFileName()) – QBrute May 31 '22 at 09:43
-
I would prefer using the `Path` solution from previous comment, but you can also use `filepath.substring(s.lastIndexOf('\\'))` (maybe adding some checking if a \ was really found) – user16320675 May 31 '22 at 09:48