How to remove the last character from a string?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Removing the last character from a string sounds trivial, but the right solution depends on what you mean by character. In many cases you only need a substring operation, but empty strings, trailing delimiters, and Unicode text can change the correct approach. A good implementation is both short and explicit about those edge cases.
The Basic Pattern
Most languages represent strings as immutable values. That means you do not modify the original string in place. Instead, you create a new string that contains everything except the last unit.
In Python, a slice handles that cleanly:
In JavaScript, slice does the same job:
In Java, use substring:
All three examples return a new string without the final code unit.
Guard Against Empty Input
The most common failure is trying to remove a last character from an empty string. Slicing in Python is forgiving, but methods in Java, C#, and similar languages can throw an exception if the length is zero.
A defensive version in C# looks like this:
That conditional makes the intended behavior clear: if the string is empty, leave it unchanged.
You can apply the same idea in Java:
Remove a Suffix Only When It Exists
Sometimes the real task is not “remove the last character,” but “remove the trailing comma” or “drop the final slash if present.” In that case, unconditional trimming is risky because it removes useful data when the suffix is missing.
Python example:
JavaScript example:
This is usually the better pattern in production code because it expresses intent. You are not dropping an arbitrary character. You are normalizing a known suffix.
Unicode Can Change the Meaning of “Character”
This topic becomes more subtle with emoji, accented text, and other Unicode sequences. A visible symbol may be composed of multiple code units. If you simply remove the last code unit, you can split the text in the middle of a visual character.
JavaScript is a classic example:
If you need to remove the last user-visible character, work with Unicode-aware iteration instead of raw slicing.
Python is generally friendlier for many Unicode cases, but even there, some user-visible symbols are made from multiple code points. If your application handles internationalized input heavily, test with real data rather than ASCII-only samples.
Prefer Clear Helper Functions
If you perform this operation often, a small helper can keep the behavior consistent across a codebase.
That approach is especially useful when you later decide to change the rule, for example to remove only a specific delimiter or to handle Unicode-aware grapheme clusters.
Common Pitfalls
The first pitfall is ignoring empty strings. In languages with strict substring bounds, that leads to runtime exceptions.
Another frequent mistake is removing the last character unconditionally when the real goal is removing a known suffix. If the input does not contain that suffix, your code deletes valid content.
Unicode is the subtle pitfall. Many developers test only with plain English letters and assume one visible symbol always equals one code unit. That assumption breaks with emoji and combined characters.
Finally, avoid writing clever one-liners when the rule needs explanation. A short helper function is easier to review than repeated substring arithmetic scattered through the codebase.
Summary
- Most languages remove the last character by creating a substring or slice.
- Guard against empty strings before using length-based substring operations.
- If you only want to remove a trailing delimiter, check for that suffix first.
- Unicode text can make “last character” more complex than “last code unit.”
- A small helper function keeps trimming behavior consistent and safer to change.

