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.

Answer from Thomas on Stack Overflow
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]","");
🌐
BeginnersBook
beginnersbook.com › 2024 › 06 › remove-special-characters-from-a-string-in-java
Remove special characters from a String in Java
June 1, 2024 - #Good -morning!"; //calling the ... matches any character that is not an // uppercase letter, lowercase letter, digit, or whitespace. String regex = "[^a-zA-Z0-9\\s]"; // Replace all special characters with an empty string // Note: There is no space between double quotes ...
Discussions

regex - remove all special characters in java - Stack Overflow
Why do you want to use the regex package? ... Your problem is that the indices returned by match.start() correspond to the position of the character as it appeared in the original string when you matched it; however, as you rewrite the string c every time, these indices become incorrect. More on stackoverflow.com
🌐 stackoverflow.com
java - How to replace special characters in a string? - Stack Overflow
I have a string with lots of special characters. I want to remove all those, but keep alphabetical characters. How can I do this? More on stackoverflow.com
🌐 stackoverflow.com
regex - how to remove special characters from string in a file using java - Stack Overflow
I have text file it contains following information.My task is to remove special symbols from that text file.My input file conatins This is sample CCNA program. it contains CCNP™. My required output More on stackoverflow.com
🌐 stackoverflow.com
regex - Remove special characters in the string in java? - Stack Overflow
OP doesn't seem to have very thorough understanding of regexen. It would be better if you explained what you did and how it works. 2014-01-12T12:21:51.657Z+00:00 ... Save this answer. ... Show activity on this post. ... Save this answer. ... Show activity on this post. I suspect that you need to assign the result (in case you're not doing that), because replaceAll() returns a new string, rather than updating the string (String is immutable): ... No need to escape the dash - in the character ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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
Here is our sample Java program to demonstrate how you can use the replaceAll() method to remove all special characters from a String in Java.
🌐
CodeSpeedy
codespeedy.com › home › how to remove special characters from a string in java
How to remove Special Characters from a String in Java - CodeSpeedy
March 11, 2022 - Regex: The regular expression for which the string has to be matched · Replacement: The string which replaces the special character ... import java.io.*; class Solution{ public static void main(String[] args)throws IOException{ BufferedReader in =new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a String \n"); String x=in.readLine(); x=x.replaceAll("[^a-zA-Z0-9]", ""); System.out.println(x); } }
🌐
Ebhor
ebhor.com › home › how to remove special characters from a string in java
How to remove special characters from a string in java - Ebhor.com
August 13, 2023 - To remove special characters we will use the String class method replaceAll(). This method takes two arguments. First is String regular expression (regex) that matched the substring that the user wants to replace.
🌐
Benchresources
benchresources.net › home › java › java 8 – how to remove special characters from string ?
Java 8 – How to remove special characters from String ? - BenchResources.Net
September 16, 2022 - 1st argument is the regex pattern – allowing only alphanumeric characters [^a-zA-Z0-9] 2nd argument is the replacement characters – empty string · Finally, print both Original String with special characters and new updated String after removing special characters to the console · package in.bench.resources.java8.string.methods; public class RemoveSpecialCharactersFromString1 { public static void main(String[] args) { // test String String str = "SJ23!$@asdf%hg%^^1*(bnb^7^()*"; System.out.println("Original String with special characters :- \n" + str); // remove special chars using regex String result = str.replaceAll("[^a-zA-Z0-9]", ""); System.out.print("\nAfter removing special characters :- \n" + result); } }
Find elsewhere
🌐
YouTube
youtube.com › watch
Remove Special Characters from a String using Regular Expression in Java #shorts #javainterview - YouTube
Remove Special Characters from a String using Regular Expression in Java #shorts #javainterview
Published   March 20, 2023
🌐
Coderanch
coderanch.com › t › 720854 › java › Remove-special-characters-regular-expression
Remove special characters/regular expression from string. (Java in General forum at Coderanch)
Knute Snortum wrote:...how about [^#]*#(.*) Gives a different output. I think OP wants the last occurrence of # followed by the word. My regex prints "ranch" for "I#love#code#ranch" and your regex prints "love#code#ranch"
🌐
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 } }
🌐
YouTube
youtube.com › watch
Remove Junk/Special Chars in a String - Java Interview Questions -2 - YouTube
How to Remove Junk/Special Characters in a String by using Regular Expression:-Remove special chars/Chinese/Japanese chars from a String.We use this Regular ...
Published   November 21, 2017
🌐
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...
🌐
Blogger
javarevisited.blogspot.com › 2016 › 02 › how-to-remove-all-special-characters-of-String-in-java.html
Javarevisited: How to remove all special characters from String in Java? Example Tutorial
public class App{ public static void main(String args[]) { String text = "This - text ! has \\ /allot # of % special % characters"; text = text.replaceAll("[^a-zA-Z0-9]", ""); System.out.println(text); String html = "This is bold"; html = html.replaceAll("[^a-zA-Z0-9\\s+]", ""); System.out.println(html); } } Output Thistexthasallotofspecialcharacters b This is bold b That's all about how to remove all special characters from String in Java.
🌐
Oracle
forums.oracle.com › ords › apexds › post › remove-all-the-special-characters-using-java-util-regex-3469
Remove all the special characters using java.util.regex - Oracle Forums
December 22, 2009 - Hi, How to remove the all the special characters in a String[] using regex, i have the following:- public class RegExpTest { private static String removeSplCharactersForNumber(String[] number) { ...
🌐
Medium
manishkrb.medium.com › remove-tags-and-special-character-68f7c468f145
Remove Tags and Special Character | by MANISHA BHARDWAJ | Medium
May 30, 2025 - public String removeTagsAndSpecialCharacter(String searchKeyword) { searchKeyword = Jsoup.parse(searchKeyword).text(); searchKeyword = searchKeywordTrim(searchKeyword); return searchKeyword.replaceAll(REGEX_ALPHA_NUMERIC, EMPTY); } ... Purpose: ...