In Java, strings are immutable objects, which means once a String is created, it cannot be changed. This fundamental concept often confuses beginners coming from languages with mutable strings. Which means instead, you must create a new string that excludes the unwanted character. If you need to remove a character from a string in Java, you cannot modify the original string directly. Whether you are cleaning user input, processing text data, or preparing strings for database storage, knowing how to remove a character from a string in Java is an essential skill for every Java developer.
Understanding String Immutability in Java
Before diving into the techniques, it is important to understand why Java handles strings this way. The String class in Java is designed to be immutable for security, performance, and thread-safety reasons. When you perform an operation that appears to change a string, Java actually creates a new String object in memory and leaves the original untouched.
This immutability means that every removal operation results in a new string instance. For small-scale applications, this has negligible impact. Still, in loops or high-frequency text processing, creating many intermediate string objects can lead to memory overhead and garbage collection pressure. That is why choosing the right removal method matters Easy to understand, harder to ignore..
Method 1: Using the replace() Method
The simplest way to remove all occurrences of a specific character is to use the replace() method. You replace the target character with an empty string, effectively deleting it from the result.
String original = "Hello World!";
String modified = original.replace('l', "");
System.out.println(modified); // Output: Heo Word!
The replace(char oldChar, char newChar) method scans the entire string and substitutes every matching character. And because you pass an empty string as the replacement, the character is removed. This approach is ideal when you want to eliminate every instance of a particular letter, digit, or symbol.
There is also replace(CharSequence target, CharSequence replacement), which works with substrings rather than single characters, but the char-based version is more direct for character removal.
Method 2: Using replaceAll() with Regular Expressions
If you need more flexibility, such as removing a character based on a pattern, replaceAll() accepts a regular expression. Here's one way to look at it: to remove all digits from a string:
String text = "Order123 is ready";
String result = text.replaceAll("\\d", "");
System.out.println(result); // Output: Order is ready
Be cautious when using replaceAll(), because the first argument is interpreted as a regex. Characters like ., *, +, ?, and \ have special meanings in regular expressions Easy to understand, harder to ignore..
Some disagree here. Fair enough.
String fileName = "document.txt";
String cleanName = fileName.replaceAll("\\.", "");
Method 3: Using StringBuilder for Index-Based Removal
When you know the exact position of the character you want to remove, StringBuilder is the most efficient tool. Unlike String, StringBuilder is mutable, so you can modify its contents without creating new objects at every step.
The deleteCharAt(int index) method removes the character at the specified position:
String original = "Java";
StringBuilder sb = new StringBuilder(original);
sb.deleteCharAt(1); // Removes 'a' at index 1
String result = sb.toString();
System.out.println(result); // Output: Jva
After making changes, always