Prev: How to get the file name from a String containing the path plusthe file name?
Next: How to convert one text file into one large String?
From: Mike Schilling on 4 Mar 2010 15:31 www wrote: > www wrote: >> Hi, >> >> I have a String "dirA/dirB/dirC/file.txt". How to use String >> manipulation technique to get "file.txt"? >> >> String fullName = "dirA/dirB/dirC/file.txt"; >> >> int index = fullName.lastIndexOf("\\"); //here is a problem, if I >> try lastIndexOf("\"), there is a compile error >> >> String fileName = fullName.subString(index); //but does not work >> >> Thank you very much. >> >> ps: I am working on linux. > > Sorry. I got it work. It was a typo: > > int index = fullName.lastIndexOf("\\"); //typo > > int index = fullName.lastIndexOf("/"); //now it is correct Better, becasue it will handle more general file name syntax (e.g. both / and \ on Wondows): File f = new File(str); String name = f.getName();
From: Roedy Green on 4 Mar 2010 21:04
On Thu, 04 Mar 2010 15:06:05 -0500, www <www(a)nospam.com> wrote, quoted or indirectly quoted someone who said : > >I have a String "dirA/dirB/dirC/file.txt". How to use String >manipulation technique to get "file.txt"? see http://mindprod.com/jgloss/filesanddirectories.html -- Roedy Green Canadian Mind Products http://mindprod.com The major difference between a thing that might go wrong and a thing that cannot possibly go wrong is that when a thing that cannot possibly go wrong goes wrong it usually turns out to be impossible to get at or repair. ~ Douglas Adams (born: 1952-03-11 died: 2001-05-11 at age: 49) |