Replace any sequence of non-letters with a single white space:

str.replaceAll("[^a-zA-Z]+", " ")

You also might want to apply trim() after the replace.

If you want to support languages other than English, use "[^\\p{IsAlphabetic}]+" or "[^\\p{IsLetter}]+". See this question about the differences.

Answer from Cephalopod on Stack Overflow
🌐
BeginnersBook
beginnersbook.com › 2024 › 06 › remove-special-characters-from-a-string-in-java
Remove special characters from a String in Java
June 1, 2024 - String regex = "[^a-zA-Z0-9\\s]"; // Replace all special characters with an empty string // Note: There is no space between double quotes return str.replaceAll(regex, ""); } }
🌐
Quora
quora.com › How-do-you-remove-special-and-space-characters-in-Java
How to remove special and space characters in Java - Quora
Answer: Java uses replaceAll() method. This method replaces each substring matching the given regular expression with the given replacement. A String strInput example shows this. Java uses String trim() method. This method removes leading and trailing whitespaces. This whitespace character unicod...
🌐
YouTube
youtube.com › watch
Java program to remove special characters, spaces and other junk from the given String text - YouTube
View Notes Here - http://www.evernote.com/l/AbFLC6kFcd9IjqZ-u7sJELQBnL5Q2HaqOkQ/
Published   November 8, 2020
🌐
Blogger
javarevisited.blogspot.com › 2016 › 02 › how-to-remove-all-special-characters-of-String-in-java.html
How to remove all special characters from String in Java? Example Tutorial
Similarly, if you String contains many special characters, you can remove all of them by just picking alphanumeric characters e.g. replaceAll("[^a-zA-Z0-9_-]", ""), which will replace anything with empty String except a to z, A to Z, 0 to 9,_ ...
🌐
Baeldung
baeldung.com › home › java › java string › remove whitespace from a string in java
Remove Whitespace From a String in Java | Baeldung
August 13, 2024 - replacing consecutive whitespace with one single space ... It’s worth mentioning that we can also first trim the input string, and then replace consecutive whitespace. So it doesn’t matter which step we take first. For the first step, we can still use replaceAll() with a regex to match consecutive whitespace characters and set one space as the replacement.
🌐
CodingTechRoom
codingtechroom.com › question › remove-white-space-special-characters-java
How to Remove White Space and Special Characters in Java - CodingTechRoom
Solution: Ensure your regex pattern accurately matches all desired whitespace and special characters. Mistake: Not handling null or empty strings before processing.
Find elsewhere
🌐
Vultr Docs
docs.vultr.com › java › examples › remove-all-whitespaces-from-a-string
Java Program to Remove All Whitespaces from a String | Vultr Docs
December 16, 2024 - Here, the replaceAll("\\s+", "") statement replaces all groups of whitespace characters in the string original with an empty string, effectively removing them. This results in the output Javaisfun.
Top answer
1 of 9
294

That depends on what you define as special characters, but try replaceAll(...):

String result = yourString.replaceAll("[-+.^:,]","");

Note that the ^ character must not be the first one in the list, since you'd then either have to escape it or it would mean "any but these characters".

Another note: the - character needs to be the first or last one on the list, otherwise you'd have to escape it or it would define a range ( e.g. :-, would mean "all characters in the range : to ,).

So, in order to keep consistency and not depend on character positioning, you might want to escape all those characters that have a special meaning in regular expressions (the following list is not complete, so be aware of other characters like (, {, $ etc.):

String result = yourString.replaceAll("[\\-\\+\\.\\^:,]","");


If you want to get rid of all punctuation and symbols, try this regex: \p{P}\p{S} (keep in mind that in Java strings you'd have to escape back slashes: "\\p{P}\\p{S}").

A third way could be something like this, if you can exactly define what should be left in your string:

String  result = yourString.replaceAll("[^\\w\\s]","");

This means: replace everything that is not a word character (a-z in any case, 0-9 or _) or whitespace.

Edit: please note that there are a couple of other patterns that might prove helpful. However, I can't explain them all, so have a look at the reference section of regular-expressions.info.

Here's less restrictive alternative to the "define allowed characters" approach, as suggested by Ray:

String  result = yourString.replaceAll("[^\\p{L}\\p{Z}]","");

The regex matches everything that is not a letter in any language and not a separator (whitespace, linebreak etc.). Note that you can't use [\P{L}\P{Z}] (upper case P means not having that property), since that would mean "everything that is not a letter or not whitespace", which almost matches everything, since letters are not whitespace and vice versa.

Additional information on Unicode

Some unicode characters seem to cause problems due to different possible ways to encode them (as a single code point or a combination of code points). Please refer to regular-expressions.info for more information.

2 of 9
72

This will replace all the characters except alphanumeric

replaceAll("[^A-Za-z0-9]","");
🌐
javaspring
javaspring.net › blog › remove-specific-characters-from-string-in-java
How to Remove Specific Characters from a String in Java: Step-by-Step Tutorial with Example — javaspring.net
The remove(String, char) overload remains available and is not deprecated. For complex regex patterns (e.g., removing characters based on position or conditional logic), use Pattern and Matcher classes for fine-grained control. Problem: Remove all digits (0-9) from "abc123def456". import java.util.regex.Matcher; import java.util.regex.Pattern; public class RemoveDigitsWithRegex { public static void main(String[] args) { String original = "abc123def456"; Pattern pattern = Pattern.compile("\\d"); // Regex for digits (0-9) Matcher matcher = pattern.matcher(original); // Replace all digits with empty string String result = matcher.replaceAll(""); System.out.println("Original: " + original); // Output: Original: abc123def456 System.out.println("Result: " + result); // Output: Result: abcdef } }
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Different ways to remove Spaces from String In Java - Java Code Geeks
August 4, 2020 - Using replaceAll() method we can replace each matching regular expression substring with the given replacement string. For example for removing all spaces, removing leading spaces, removing trailing spaces and so on.
🌐
Javatpoint
javatpoint.com › how-to-remove-special-characters-from-string-in-java
How to Remove Special Characters from String in Java - Javatpoint
How to Remove Special Characters from String in Java with oops, string, exceptions, multithreading, collections, jdbc, rmi, fundamentals, programs, swing, javafx, io streams, networking, sockets, classes, objects etc,
🌐
Scaler
scaler.com › home › topics › remove whitespace from string in java
Remove Whitespace From String in Java - Scaler Topics
January 6, 2024 - Also, Java provides numerous in-built methods to perform this task. To list a few: trim(): It removes the leading and trailing spaces present in the string. You can see this in the image below: strip(): It removes the leading and trailing spaces present in the string.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-remove-all-white-spaces-from-a-string-in-java
How to Remove all White Spaces from a String in Java? - GeeksforGeeks
For this, we will use Character.isWhitespace(c). ... // Java program to demonstrate how to remove all white spaces from a string import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { String s = " Geeks for ...
Published   July 11, 2025
🌐
W3Schools
w3schools.com › java › java_howto_remove_whitespace.asp
Java How To Remove Whitespace from a String
Data Types Numbers Booleans Characters Real-Life Example Non-primitive Types The var Keyword Code Challenge Java Type Casting Java Operators · Operators Arithmetic Assignment Comparison Logical Precedence Code Challenge Java Strings · Strings Concatenation Numbers and Strings Special Characters Code Challenge Java Math Java Booleans
🌐
YouTube
youtube.com › watch
How to remove Junk or special characters in a String (Core Java Interview Question #532) - YouTube
In this session, I have explained and practically demonstrated in Java, how to remove Junk or special characters in Java by using replaceAll() method by pass...
Published   August 2, 2024
🌐
YouTube
youtube.com › watch
Remove special characters from string - YouTube
Hi Friends, #GainJavaKnowledgeWelcome to this channel Gain Java Knowledge. We are providing best content of Java in vide...
Published   July 16, 2022