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 - In order to remove special characters, we can use regex along with the replaceAll() method. public class RemoveSpecialCharacters { public static void main(String[] args) { //given string String input = "Hello! How are you? #Good -morning!"; //calling the user defined method for removing special ...
🌐
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 - To remove special characters from the given String, use replaceAll() method of String which accepts 2 input-arguments –
🌐
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,_ ...
🌐
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
Java Program To Remove Special Characters From Given String | Ashok IT - YouTube
#RemovalSpecialChars #ashokit ✍️✍️ Register Here For Online Training : https://bit.ly/4dBYJbX ** For Online Training ► Call: +91-6301921083Subscribe to our...
Published   May 16, 2020
🌐
Just Academy
justacademy.co › blog-detail › how-to-remove-special-characters-from-a-string-in-java
How to Remove Special Characters from a String in Java by Roshan Chaturvedi | JustAcademy
To remove special characters from a string in Java, you can use regular expressions with the `replaceAll()` method to match and replace non-alphanumeric characters with an empty string.
🌐
Medium
manishkrb.medium.com › remove-tags-and-special-character-68f7c468f145
Remove Tags and Special Character | by MANISHA BHARDWAJ | Medium
May 30, 2025 - This looks like a custom method (not standard Java). Based on the name, it probably trims the string to remove leading and trailing whitespace, maybe also handles other whitespace cleanups. ... Applies a regex replacement on the trimmed text. REGEX_ALPHA_NUMERIC is likely a pattern that matches non-alphanumeric characters or special characters. The method replaces those characters with EMPTY (which usually is defined as ""), effectively removing special characters from the string.
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,
🌐
Code Beautify
codebeautify.org › blog › remove-special-characters-from-string-java
Remove Special Characters From String Java
February 22, 2024 - Examplе Usagе: For dеmonstration purposеs, an еxamplе string with spеcial charactеrs is providеd in thе main mеthod. You can rеplacе this string with your own input.
🌐
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
🌐
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
🌐
Quora
quora.com › How-do-you-remove-a-non-UTF-8-character-from-a-string-in-Java
How to remove a non- UTF-8 character from a string in Java - Quora
Answer (1 of 3): I write a string validation using the systems default charset. I will most frequently use a switch/case block to filter & replace. I only had to really do this once in an extremely fucked up code bases where Chinese characters and Turkish characters shows up as a vertical rectang...
🌐
Coderanch
coderanch.com › t › 401019 › java › remove-special-characters-String
remove special characters from String (Beginning Java forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... I have String test = " i am 'fine". hope 'you "are"; How can i remove wherever my String has single or double quotes ?
🌐
GitHub
gist.github.com › rponte › 893494
Removing accents and special characters in Java: StringUtils.java and StringUtilsTest.java · GitHub
Local para mim funciona perfeito, subo no Websphere 8.5 ele insiste em converter Ç para A. Ex.: CONSOLAÇÂO fica CONSOLAAO · Será encode do websphere? Copy link · Copy Markdown · Unfortunately, this removes "ß" from the string · Copy link · Copy Markdown · +1 · Copy link · Copy Markdown · Author · https://www.baeldung.com/java-remove-accents-from-text ·