Java ProgramsStringsRemove Special Characters from a String

Remove Special Characters from a String in Java

beginner·  Strings  ·  String Manipulation

Problem

A special character is anything that isn't a letter, digit, or whitespace — punctuation and symbols like @, #, or !.

Given a string, remove every special character from it.

Input
Hello@World#2024!
Output
HelloWorld2024

Java Program

Java
public class RemoveSpecialCharacters { public static void main(String[] args) { String str = "Hello@World#2024!"; StringBuilder result = new StringBuilder(); for (char c : str.toCharArray()) { // Keep letters, digits, and whitespace; drop everything else if (Character.isLetterOrDigit(c) || Character.isWhitespace(c)) { result.append(c); } } System.out.println(result.toString()); } }

Output

HelloWorld2024

Core Logic

Keeping only characters that are letters, digits, or whitespace — and dropping everything else — filters out the special characters in one pass.

How It Works
  1. 1A for-each loop visits each character of the string in turn.
  2. 2Character.isLetterOrDigit(c) keeps ordinary letters and numbers.
  3. 3Character.isWhitespace(c) keeps spaces, tabs, and newlines.
  4. 4A character passing either check is appended to a StringBuilder; anything else — punctuation and symbols — is skipped entirely.
For "Hello@World#2024!", the @, #, and ! characters are skipped, producing "HelloWorld2024".
💡

Key Point: This is the same 'everything left over' definition used to count special characters — here the special ones are dropped instead of tallied.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Each character is checked once, and the result buffer grows to hold every letter, digit, and whitespace character kept.

Key Concepts

Character.isLetterOrDigit()Character.isWhitespace()StringBuilder

Approach 2: Regex Replace

Java
public class RemoveSpecialCharactersRegex { public static void main(String[] args) { String str = "Hello@World#2024!"; // Removes anything that isn't a letter, digit, or whitespace String result = str.replaceAll("[^a-zA-Z0-9\\s]", ""); System.out.println(result); } }

Output

HelloWorld2024

Core Logic

A regex character class can express 'not a letter, digit, or whitespace' directly, removing every match in one call.

How It Works
  1. 1str.replaceAll("[^a-zA-Z0-9\\s]", "") matches any character that is NOT a letter, digit, or whitespace.
  2. 2Every matched character is replaced with an empty string, which deletes it.
  3. 3What's left is a new string containing only letters, digits, and whitespace.
Replacing every non-alphanumeric, non-whitespace character in "Hello@World#2024!" removes @, #, and !, producing "HelloWorld2024".
💡

Key Point: The ^ at the start of the character class negates it — [^...] matches any character NOT listed, the opposite of a normal character class.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: replaceAll() still has to scan the whole string and build a new one holding every kept character.

Key Concepts

regexreplaceAll()

Approach 3: Java 8

Java
import java.util.stream.Collectors; public class RemoveSpecialCharactersStream { public static void main(String[] args) { String str = "Hello@World#2024!"; // Keeps letters, digits, and whitespace; drops everything else String result = str.chars() .filter(c -> Character.isLetterOrDigit(c) || Character.isWhitespace(c)) .mapToObj(c -> String.valueOf((char) c)) .collect(Collectors.joining()); System.out.println(result); } }

Output

HelloWorld2024

Core Logic

The same 'letter, digit, or whitespace' check can filter a stream of character codes, then join what's left.

How It Works
  1. 1str.chars() returns an IntStream of the string's character codes.
  2. 2.filter(c -> Character.isLetterOrDigit(c) || Character.isWhitespace(c)) keeps only the characters that aren't special.
  3. 3.mapToObj(c -> String.valueOf((char) c)) converts each surviving code back into a one-character String.
  4. 4.collect(Collectors.joining()) concatenates them all back into a single result string.
Filtering "Hello@World#2024!" drops @, #, and !, and joining what's left reassembles "HelloWorld2024".
💡

Key Point: The filter condition is the exact same boolean expression the loop version checks — streams just change how it's applied across the string.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: The stream still visits every character once, and Collectors.joining() builds a result string holding every kept character.

Key Concepts

Streamchars()filter()Collectors.joining()

Related Programs