From 32dba7dd0699f7581b3f871cd21ff74e1d0d526f Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Wed, 25 Nov 2020 00:03:34 +0100 Subject: [PATCH] remove file extensions --- .../MyFilenameUtil.java | 14 ++++ .../FileNameDelExtensionUnitTest.java | 72 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 core-java-modules/core-java-io-3/src/main/java/com/baeldung/filenamewithoutextension/MyFilenameUtil.java create mode 100644 core-java-modules/core-java-io-3/src/test/java/com/baeldung/filenamewithoutextension/FileNameDelExtensionUnitTest.java diff --git a/core-java-modules/core-java-io-3/src/main/java/com/baeldung/filenamewithoutextension/MyFilenameUtil.java b/core-java-modules/core-java-io-3/src/main/java/com/baeldung/filenamewithoutextension/MyFilenameUtil.java new file mode 100644 index 0000000000..102c454c49 --- /dev/null +++ b/core-java-modules/core-java-io-3/src/main/java/com/baeldung/filenamewithoutextension/MyFilenameUtil.java @@ -0,0 +1,14 @@ +package com.baeldung.filenamewithoutextension; + +public class MyFilenameUtil { + private MyFilenameUtil() {} + + public static String removeFileExtension(String filename, boolean removeAllExtensions) { + if (filename == null || filename.isEmpty()) { + return filename; + } + + String extPattern = "(?