How do I trim a file extension from a String in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Removing a file extension from a Java string looks trivial until you hit edge cases such as names with multiple dots, paths that contain directories, or hidden files that begin with a dot. The safest solution depends on whether you are handling a plain filename or a full path.
For a plain filename, the usual approach is to find the last dot and remove everything after it. The important part is deciding which dots actually represent an extension.
Basic Approach with lastIndexOf
For most filenames, the simplest working solution is to trim everything after the last dot:
This prints report, archive.tar, and README. For many applications, that behavior is exactly right.
Handle Hidden Files and Paths Correctly
The naive version treats any dot as an extension separator. That breaks for Unix-style hidden files such as .gitignore, where the leading dot is part of the name, not an extension.
It also helps to avoid trimming dots that appear in directory names. This version handles both concerns for a path-like string:
Now .gitignore stays unchanged, while ordinary extensions are removed.
Decide What “Extension” Means in Your App
There is no single universal rule for compound names such as archive.tar.gz. Some programs consider only gz to be the extension. Others treat tar.gz as a meaningful combined extension.
If you want to remove only the last segment, lastIndexOf('.') is correct. If you want to strip a known compound suffix, test for that suffix explicitly:
That is more explicit and easier to maintain than trying to infer every special case from dots alone.
Avoid Overcomplicating It
You do not need regular expressions for the common case. String slicing is clearer, faster to read, and easier to debug. Reach for a library helper only if your project already depends on one and the helper matches your filename rules.
The critical design step is writing down the behavior you want for no-extension files, hidden files, compound suffixes, and full paths.
Common Pitfalls
- Removing everything after the first dot instead of the last one.
- Treating
.gitignoreas if it had an extension. - Forgetting that path separators can appear before the final filename dot.
- Assuming
tar.gzshould always be removed as one combined suffix. - Using a regex for a simple substring operation and making the logic harder to test.
Summary
- The normal Java solution is to use
lastIndexOf('.')andsubstring. - Hidden files and full paths need extra checks so that non-extension dots are preserved.
- Decide whether your app treats compound names such as
tar.gzas one suffix or two. - Simple string logic is usually clearer than a regular expression.
- Define the edge-case behavior first, then implement the smallest method that matches it.
Related reading
- How do I turn a String into a InputStreamReader in java?
- How do I update an entity using spring-data-jpa?
- How do I update the element at a certain position in an ArrayList?
- How do I use a PriorityQueue?
- How do I use JAXB annotations with Spring RestTemplate?
- How do I use optional parameters in Java?
- How do I use reflection to invoke a private method?
- How do I use Spring Boot to serve static content located in Dropbox folder?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.