Can you please tell me that how to remove file extension from the file path.
i.e I have a file stored in the path D:/test/aaa.zip and I want file name from this path
I have written a code from which I get a file name aaa.zip but i dont want the file name with extension..
i want only aaa and not aaa.zip...
so can you please tell me how i can get filename without extension...
Scott Bravo wrote:
If your need is extensive then you can also override the getName() method and apply ulf's suggested functionality to the overrided method..
Ehm, please don't. How would you get the name with the extension that way?
The best way is using a static utility method; this also allows you to reuse the code.
As Ulf suggested, lastIndexOf('.') will help you. However, you should consider two cases:
- the last index is -1, meaning there is no dot in the name. There is no extension.
- the last index is 0, meaning the only dot is the first character of the name. This can be considered as either "there is no extension", or "there is only an extension". The choice is up to you, but most explorer type programs use the first choice.