Java String Program · S30
Remove a Given Character from a String in Java
Copy every character except the one you want to delete. Strings never change in Java, so the result is always a new string.
Problem Statement
Write a method that returns a copy of a string with every occurrence of a given character removed.
The comparison is case-sensitive, so removing 'a' does not remove 'A'. If the character does not occur, the string is returned unchanged.
Input/Output Examples
"programming", 'm'Output"prograing"Explanation: both m's are removed."Apple", 'a'Output"Apple"Explanation: the string contains A but no lowercase a."aaa", 'a'Output""Explanation: every character matches, so the result is empty.Visual Explanation
'm'; matches are dropped and everything else is copied in order.A single comparison per character decides whether it is copied. Because the kept characters are appended in reading order, the result keeps the original order.
Approaches
The loop shows the logic explicitly. String.replace() does the same in one call.
Approach 1: StringBuilder Loop (Recommended)
Idea: append every character that is not equal to the target.
- Create a
StringBuilderwith the input length as capacity. - For each character, append it if it differs from the target.
- Return
toString().
Time: O(n) · Space: O(n).
Approach 2: String.replace()
Idea: replace the character, as a one-character string, with an empty string.
- Convert the target with
String.valueOf(target). - Call
text.replace(target, ""). - The literal replacement removes every occurrence.
Time: O(n) · Space: O(n).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: StringBuilder Loop (Recommended)
Easy to extend, for example to remove a set of characters or to ignore case.
public class RemoveCharLoop {
// Copies every character except the one to remove (case-sensitive)
static String removeChar(String text, char target) {
StringBuilder result = new StringBuilder(text.length());
for (char c : text.toCharArray()) {
if (c != target) {
result.append(c);
}
}
return result.toString();
}
public static void main(String[] args) {
Object[][] cases = {{"programming", 'm'}, {"Apple", 'a'}, {"aaa", 'a'}};
for (Object[] c : cases) {
System.out.println("\"" + c[0] + "\", '" + c[1] + "' -> \"" + removeChar((String) c[0], (Character) c[1]) + "\"");
}
}
}
"programming", 'm' -> "prograing" "Apple", 'a' -> "Apple" "aaa", 'a' -> ""
Time complexity: O(n) · Space complexity: O(n).
Approach 2: String.replace()
replace(CharSequence, CharSequence) treats its argument literally. replaceAll() would treat it as a regex, which breaks for characters such as . or $.
public class RemoveCharReplace {
// replace(CharSequence, CharSequence) replaces every literal match; no regex is involved
static String removeChar(String text, char target) {
return text.replace(String.valueOf(target), "");
}
public static void main(String[] args) {
Object[][] cases = {{"programming", 'm'}, {"Apple", 'a'}, {"aaa", 'a'}};
for (Object[] c : cases) {
System.out.println("\"" + c[0] + "\", '" + c[1] + "' -> \"" + removeChar((String) c[0], (Character) c[1]) + "\"");
}
}
}
"programming", 'm' -> "prograing" "Apple", 'a' -> "Apple" "aaa", 'a' -> ""
Time complexity: O(n) · Space complexity: O(n).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| StringBuilder Loop | O(n) | O(n) |
| String.replace() | O(n) | O(n) |
Here n is the length of the string. Both approaches read every character once and build a new string of at most n characters.
Interview Tips and Recommended Approach
- Know the difference between
replace()andreplaceAll(). OnlyreplaceAll()uses regex; removing'.'with it would remove every character. - Avoid
deleteCharAt()in a forward loop. Deleting shifts the following characters, so the loop index skips one; loop backwards if you must delete in place. - Clarify case sensitivity. To ignore case, compare with
Character.toLowerCase()on both sides. - Remember immutability. The original string is unchanged; you must use the returned value.
Recommended Approach: StringBuilder Loop
Why: it is linear, has no regex pitfalls and makes the comparison rule visible, which helps when the interviewer changes the requirements.