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 - 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, ""); } }
🌐
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 - It will remove all special characters except small a-z and A-Z. ... It removes characters other than small and capital alphabets. Here you can see there is a digit in a String that is also removed. If you want not to remove digits then you have to modify your Regular expression. In above example we want to remove special characters. String also contains digits we want to keep digits. ... Sometimes we want to keep some special characters with string-like comma(,) or full stop (.).
🌐
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,_ ...
🌐
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 - In this article, we will learn how to remove special characters from the given String Remove special characters from given String : Using Java 1.7 Read More
🌐
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...
Find elsewhere
🌐
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,
🌐
Coderanch
coderanch.com › t › 720854 › java › Remove-special-characters-regular-expression
Remove special characters/regular expression from string. (Java in General forum at Coderanch)
It was owl file i am converting to string and trying to remove special characters. Else i am saving the raw output in txt file and trying to achive my desired output ... The code partially works. I am getting the output as Not like · alloyAlsoKnownAs, range, DesignationType Also can i able to replace replaceString to target.toString()? ... Can i able to change the string to target.string()? I tried changing and i got the following error java.lang.StringIndexOutOfBoundsException: String index out of range: -136889
🌐
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
🌐
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
🌐
Quora
quora.com › How-do-you-remove-special-characters-from-a-string-in-Java-except-space
How to remove special characters from a string in Java except space - Quora
Answer: Java Strings are immutable, so you create a new string that contains only the characters you want to keep. Do this using a StringBuilder and iterating over the characters from the original string and use a condition to create a filter that decides if the character should be appended to th...
🌐
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 = …
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-remove-character-string
Java String Replace: How to Replace Characters and Substrings | DigitalOcean
February 20, 2025 - Learn how to replace characters and substrings in Java using replace(), replaceAll(), and replaceFirst(). See code examples for string manipulation.
🌐
Alvin Alexander
alvinalexander.com › java › java-strip-characters-string-letters-numbers-replace
Java: How to strip unwanted characters from a string | alvinalexander.com
Here's a quick line of Java code ... = aString.replaceAll("[^a-zA-Z]",""); As you might be able to tell by looking at that line of code, the String replaceAll method does the hard work for you, essentially removing the characters in the output stream that you want to remove ...
🌐
GitHub
gist.github.com › rponte › 893494
Removing accents and special characters in Java: StringUtils.java and StringUtilsTest.java · GitHub
Removing accents and special characters in Java: StringUtils.java and StringUtilsTest.java - StringUtils.java
🌐
Coderanch
coderanch.com › t › 230886 › Remove-Special-Characters-String
Remove Special Characters from String (Java Micro Edition forum at Coderanch)
September 29, 2008 - I need the string to equal "1234567890". Thanks! Drew ... Drew, The easiest thing I can think of is to use String's replaceAll method. You could use a regular expression to match all characters except digits and replace them with an empty string. [OCP 21 book] | [OCP 17 book] | [OCP 11 book] ...