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, ""); } } Original: Hello! How are you? #Good -morning! After removing special characters: Hello How are you Good morning
🌐
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,
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-remove-character-string
Java String Replace: How to Replace Characters and Substrings | DigitalOcean
February 20, 2025 - Both methods are effective for replacing multiple substrings in a Java string. 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().
🌐
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.
🌐
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 –
🌐
Websparrow
websparrow.org › home › remove all special character from string in java
Remove All Special Character from String in Java - Websparrow
May 15, 2023 - package org.websparrow; public class RemoveSpecialCharacters { public static void main(String[] args) { String str = "!W@eb#s$pa%rr*,o:w"; String finalStr = str.replaceAll("[^0-9a-zA-Z]", ""); System.out.println("Original string: " + str); System.out.println("Final string: " + finalStr); } } Note: The regular expression [^0-9a-zA-Z] matches any character that is not a letter(uppercase or lowercase) or a number.
Find elsewhere
🌐
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 - 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); } } ... In this output, as you can see the regex written in the program states that any character other than A-Z, a-z, or 0-9 gets removed.
🌐
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
🌐
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 = …
🌐
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
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
🌐
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 · Raw · StringUtils.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
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
🌐
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...