18

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!

Addev
  • 31,819
  • 51
  • 183
  • 302

7 Answers7

42

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.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 5
    However, it will also [keep](http://ideone.com/NS2iH) `.kitty.jpg`, without removing the `.jpg`. This may or may not be desired. – Matthew Flaschen Sep 25 '11 at 07:05
  • 3
    You can do `name.lastIndexOf(".") > 0` instead of `name.indexOf(".") > 0` and it will remove the `.jpg` from `.kitty.jpg` while keeping it as `.kitty` – Sub 6 Resources Aug 10 '17 at 21:55
20

Use Apache Commons IO, FilenameUtils.getBaseName(String filename) or removeExtension(String filename), if you need the path too.

palacsint
  • 28,416
  • 10
  • 82
  • 109
8

Here is a slightly more robust version:

public static String stripExtension(final String s)
{
    return s != null && s.lastIndexOf(".") > 0 ? s.substring(0, s.lastIndexOf(".")) : s;
}

Gracefully handles null, cases where there is nothing that looks like an extension and doesn't molest files that start with a ., this also handles the .kitty.jpg problem as well.

This will handle most general cases in a controlled environment, if the suffix isn't actually an extension, just something . separated then you need to add some checking to see if the trailing part actually is an extension you recognize.

4
myString = myString.replaceAll("\\.\\w+", "");
Community
  • 1
  • 1
Sergey K
  • 4,071
  • 2
  • 23
  • 34
3
String result = filename.substring(0, filename.lastIndexOf("."))

Do check to see if the filename has a . before calling substring().

Bala R
  • 107,317
  • 23
  • 199
  • 210
2
String name = filename.lastIndexOf('.') > filename.lastIndexOf(File.separatorChar) ?
     filename.substring(0, filename.lastIndexOf('.'))
   : filename;

The comparison checks that the filename itself contains a ., otherwise the string is returned unchanged.

Howard
  • 38,639
  • 9
  • 64
  • 83
0
Pattern suffix = Pattern.compile("\\.\\p{Alnum}+$");
Matcher m = suffix.matcher(filename);
String clearFilename = m.replaceAll("");

The problem of this solution is that, depending of your requirements, you need to extend it to support composed extensions, like filename.tar.gz, filename.tar.bz2

[]'s

And Past

Andre Pastore
  • 2,841
  • 4
  • 33
  • 44
  • 1
    Remember that if you need to apply this function often, it's better to use compiled regex (Pattern.compile). If you use String.replace and positioning, it will be processed every execution and consume more CPU time. – Andre Pastore Sep 24 '11 at 21:02
  • 2
    Came here looking for an answer like this, but as much as I prefer regex's for readability `path.substring(0, path.lastIndexOf('.'))` turns out to be orders of magnitude faster than `pattern.matcher(path).replaceFirst("")`, even with a precompiled pattern. – MithrilTuxedo Oct 06 '16 at 19:58