Compare strings in java and remove the part of string where they are identical
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When two Java strings share the same beginning or ending, a common task is to trim the identical part and keep only the differing part. The important first step is to define what "identical part" means, because removing a common prefix is very different from removing any matching characters anywhere in the string.
Remove a Common Prefix
The most common interpretation is "remove the shared prefix." That means comparing both strings from left to right until the first mismatch.
Here is a small utility that returns the remaining unmatched parts:
The output is:
This works because both strings share the prefix "abc", so that part is removed from both.
Remove a Common Suffix
Sometimes the identical part is at the end rather than the beginning. In that case, scan from right to left.
If the inputs are "report-final.txt" and "image-final.txt", the common suffix is "-final.txt", so the remaining parts are "report" and "image".
This is a different problem from prefix trimming, so it deserves a separate method rather than overloading one function with unclear behavior.
When You Need More Than Prefix or Suffix Matching
If you want to remove matching characters in the middle of both strings, the problem becomes more complex. For example, removing the longest common subsequence is not the same as trimming a prefix or suffix. That requires a different algorithm entirely.
For many business cases, prefix trimming is enough. Common examples include:
- removing a shared file path root
- stripping a common namespace prefix
- comparing version strings after a shared start
When the task is really "find the first position where they differ," a prefix-based solution is the right one and stays easy to reason about.
A Reusable Comparison Utility
If you want a single helper for debugging or comparison features, you can make the result more explicit:
This style is useful because it keeps the removed part visible instead of discarding it immediately. In debugging tools and diff views, that extra context is often more useful than only returning the remainders.
Common Pitfalls
The biggest mistake is not defining the match rule clearly. Shared prefix, shared suffix, longest common substring, and longest common subsequence are different problems with different algorithms.
Another common issue is forgetting edge cases. Empty strings, one fully matching string, or no common prefix at all should all return sensible results without throwing exceptions.
Case sensitivity also matters. Java String comparison is case-sensitive by default, so "ABC" and "abc" do not share a common prefix unless you normalize them first.
Finally, be careful with Unicode text if your application works at the user-visible character level. charAt operates on UTF-16 code units, which is usually fine for ASCII-like data but can be surprising for some Unicode characters.
Summary
- For most cases, "remove the identical part" means trimming a common prefix or suffix.
- A left-to-right scan solves common-prefix removal in linear time.
- A right-to-left scan solves common-suffix removal.
- Do not use prefix logic for middle-string matching problems such as longest common subsequence.
- Define the exact comparison rule before writing the code.

