Java – Strip extension from file name

A quick Java method to strip the extension from a file name.


* Strips extension from the file name.
* @param fileName
*            the file name
* @return the string
public static String stripFileExtension(String fileName) {
int dotInd = fileName.lastIndexOf(‘.’);

// if dot is in the first position,
// we are dealing with a hidden file rather than an extension
return (dotInd > 0) ? fileName.substring(0, dotInd) : fileName;


Need help? Let me take care of your IT issues.

Share this page

Scroll to Top