How to split filename from file extension in Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Swift, the safest way to split a filename from its extension is to use path-aware APIs rather than manual string splitting. Foundation already understands file paths, multiple dots, and common edge cases better than a raw split(separator: ".") call.
Use URL path utilities
If you have a path or filename string, turn it into a file URL and ask Foundation for the pieces:
This is usually the best solution because it is path-aware. It does not just split on the first or last dot blindly; it uses Foundation's path rules.
If you only have a bare filename rather than a full path, the same approach still works:
Notice that only the last extension is removed. That is often exactly what you want.
NSString path helpers also work
Older Swift code often uses NSString path methods, and they are still valid:
This is especially common in Swift 3 and early Foundation-heavy codebases. Under the hood, it is still relying on path semantics rather than naive string splitting, which is the important part.
Why manual splitting is often wrong
A tempting solution looks like this:
The problem is that filenames are messier than they seem:
- '
report.final.pdfhas more than one dot' - '
.gitignorestarts with a dot but is not always treated like a normal extension case' - '
archive.tar.gzmay conceptually have more than one suffix' - full paths can contain dots in directory names too
If you manually split strings, you end up reimplementing path logic badly.
Handle dotfiles and no-extension cases carefully
Two cases deserve explicit attention.
No extension:
Dotfile:
The exact result for dotfiles can surprise people because a leading dot is part of Unix-style naming conventions, not just an extension separator. That is one more reason to trust Foundation path APIs instead of hand-written parsing.
If you need every extension segment
Sometimes you do want more control, for example to distinguish archive.tar.gz into base name archive and compound extension tar.gz. In that case, Foundation's default single-extension behavior may not be enough, so you can layer custom logic on top:
This is one of the few cases where manual splitting is reasonable, because you are explicitly implementing a special rule rather than pretending it is the general case.
Common Pitfalls
The biggest mistake is using plain string splitting for every filename problem. That breaks easily on multi-dot names and full paths.
Another common issue is forgetting that pathExtension returns only the last extension segment. That is correct behavior, but it may not match your business rule for files such as archive.tar.gz.
People also test only simple filenames and forget dotfiles, files without extensions, or paths that include directories.
Finally, do not confuse a path with a display name. If you only want the last file component, call lastPathComponent rather than trying to strip directories manually.
Summary
- Prefer
URL(fileURLWithPath:),deletingPathExtension(), andpathExtension. - '
NSStringpath helpers are also valid, especially in older Swift codebases.' - Avoid manual
split(separator: ".")for general filename parsing. - Remember that Foundation removes only the last extension by default.
- Add custom logic only when you explicitly need compound extensions such as
tar.gz.
Related reading
- How to split string into substrings on iOS?
- How to start an application using Android ADB tools
- How to start new activity on button click
- How to stop EditText from gaining focus when an activity starts in Android?
- How to stop unwanted UIButton animation on title change?
- How to store an image in core data
- How to store custom objects in NSUserDefaults
- How to store user information with DynamoDB and Cognito using Facebook authentication with iOS SDK
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free 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.