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;
}

84 views

Need help? Let me take of your IT issues.

Share this page

Share on facebook
Share on linkedin
Share on twitter
Share on print
Share on email
Scroll to Top

By continuing to use this website, you consent to the use of cookies in accordance with our Cookie Policy