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 - After removing special characters: Hello How are you Good morning ยท You can adjust the regular expression to match specific types of special characters based on your requirements. Refer regex tutorial. If you want to remove whitespace from the string, you can use trim() method. Refer: trim() ...
๐ŸŒ
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,_ ...
๐ŸŒ
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,
๐ŸŒ
YouTube
youtube.com โ€บ sdet- qa
Frequently Asked Java Program 24: How To Remove Junk or Special Characters in String - YouTube
Topic : How To Remove Junk or Special Characters in String #########################Udemy Courses: #########################Manual Testing+Agile with Jira To...
Published ย  October 18, 2019
Views ย  85K
Find elsewhere
๐ŸŒ
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
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-remove-character-string
Java String Replace: How to Replace Characters and Substrings | DigitalOcean
February 20, 2025 - The choice between them depends on the complexity of the replacements and personal preference. In this article, you learned various ways to remove characters from strings in Java using methods from the String class, including replace(), replaceAll(), replaceFirst(), and substring().
๐ŸŒ
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.
๐ŸŒ
Medium
manishkrb.medium.com โ€บ remove-tags-and-special-character-68f7c468f145
Remove Tags and Special Character | by MANISHA BHARDWAJ | Medium
May 30, 2025 - Remove Tags and Special Character โœ…Sure! Let me explain your Java method implementation step-by-step: public String removeTagsAndSpecialCharacter(String searchKeyword) { searchKeyword = โ€ฆ
๐ŸŒ
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 } }
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]","");
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ remove-characters-alphabets-string
Remove all characters other than alphabets from string - GeeksforGeeks
#include <bits/stdc++.h> using namespace std; // Function to remove special characters // and store it in another variable void removeSpecialCharacter(string s) { string t = ""; for (int i = 0; i < s.length(); i++) { if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) { t += s[i]; } } cout << t << endl; } int main() { string s = "$Gee*k;s..fo, r'Ge^eks?"; removeSpecialCharacter(s); return 0; } ... import java.util.*; class GFG { // Function to remove special characters // and store it in another variable static void removeSpecialCharacter(String s) { String t = ""; for (int i = 0; i < s.length(); i++) { if ((s.charAt(i) >= 'a' && s.charAt(i) <= 'z') || (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z')) { t += s.charAt(i); } } System.out.println(t); } public static void main(String[] args) { String s = "$Gee*k;s..fo, r'Ge^eks?"; removeSpecialCharacter(s); } }
Published ย  July 31, 2023
๐ŸŒ
Java String
javastring.net โ€บ home โ€บ how to remove whitespace from string in java
How to Remove Whitespace from String in Java
August 9, 2019 - Case 2: String contains only space ... str ==> "HelloJava" There are many different types of whitespace characters. So using replace() method is not intuitive if you donโ€™t know what all special whitespace characters are present ...
๐ŸŒ
Java Code Geeks
javacodegeeks.com โ€บ home โ€บ core java
Different ways to remove Spaces from String In Java - Java Code Geeks
August 4, 2020 - Similar to strip method stripTrailing also uses Character.isWhitespace(int) for identifying white spaces. ... Added from java 1.5, This method is used to replace each target substring with the specified replacement string. This method replaces all matching target elements. Note: One more method replace(char oldChar, char newChar) is present in java string class. The only differences is that this method takes single character as target and replacement.
๐ŸŒ
Tpoint Tech
tpointtech.com โ€บ how-to-remove-special-characters-from-string-in-java
How to Remove Special Characters from String in Java?
February 12, 2025 - A character which is not an alphabet or numeric character is called a special character. We should remove all the special characters from the string so that ...