Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the extension of a file [duplicate]

Tags:

java

regex

Given a filename like:

 package.zip    image.jpeg   video.avi etc

I'd like to remove the extension if exists. How can I make this in Java? THanks!

like image 441
Addev Avatar asked Sep 24 '11 20:09

Addev


People also ask

How do I remove a double filename extension?

Open File Explorer and click View tab, Options. In Folder Options dialog, move to View tab, untick Hide extensions for known file types option, OK. Then you will se file's extension after its name, remove it.

Does Windows 10 have a duplicate file remover?

Answer: No, Windows 10 does not have a duplicate finder in it yet.


1 Answers

Something like

if (name.indexOf(".") > 0)
    name = name.substring(0, name.lastIndexOf("."));

The index check avoids turning hidden files like ".profile" into "", and the lastIndexOf() takes care of names like cute.kitty.jpg.

like image 133
Ernest Friedman-Hill Avatar answered Sep 24 '22 06:09

Ernest Friedman-Hill