Thursday, February 9, 2017

How to remove all special characters from String in Java

You can use a regular expression and replaceAll() method of java.lang.String class to remove all special characters from String. A special character is nothing but characters like ! #, %, etc. Precisely, you need to define what is a special character for you. Once you define that you can use a regular expression to replace those characters with empty String, which is equivalent to removing all special characters from String. For example, suppose, your String contains some special characters e.g. "Awesome!!!" and you want to remove those !!! to reduce some excitement, you can use replaceAll("!", "") to get rid of all exclamation mark from String.

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,_ and dash. Let's see a couple fo examples to remove all special characters from String in Java.

By the way, if you are a complete beginner into Regular expression and don't understand these magical symbols then I highly recommend you join The Complete Regular Expression course for beginners course on Udemy. It's a great course to learn everything about RegEx you want to know and it's also very affordable, you can buy in just $10 on Udemy sales which happens every now and then.

Java Program to remove all special characters from String

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. Since String is Immutable in Java, make sure you to store the String returned by the replaceAll() method, this is your output i.e. String without any special characters.

You should spend some time sharing your regular expression skill, as it's one of the powerful tools for debugging and troubleshooting. It is also one thing which separates the average programmers to good programmers. Mastering Regular Expressions is one of the great books to sharpen your regex skills.

How to remove all special characters from String in Java

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]", "");

String html = "This is bold";
html = html.replaceAll("[^a-zA-Z0-9\\s+]", "");


b This is bold b

That's all about how to remove all special characters from String in Java. As I said, you can use the replaceAll() method of String along with regular expression to get rid of unwanted characters. You can define characters you want or remove in the regular expression as shown in our example. Let me know if you have any doubt.

Further Reading
Complete Java Masterclass
Regular Expression Fundamentals
Java In-Depth: Become a Complete Java Engineer!

Related Java String Tutorials for Further Reading
  • How to reverse String in Java without Recursion? (answer)
  • How to convert a char to String to Java? (answer)
  • How to compare two String object in Java? (answer)
  • When to use the intern() method of String in Java? (answer)
  • How to convert Double to String in Java? (solution)
  • Top 20 String Algorithm Questions from Coding Interviews (read here)
  • How to find all permutations of a String in Java? (solution)
  • How to check if two String are Anagram in Java (answer)
  • Cracking the Coding Interview: 150 Programming Questions and Solutions (answer)


Unknown said...

Just in case your language uses more than the few ASCII characters from a to z, Unicode properties are pretty useful:

K2 said...

thanks a lot!

Anonymous said...

Thanks man, simple yet effective solution.

Anonymous said...

Thanks, you saved me a great amount of time fixing this issue.

Sonu said...


Unknown said...

thanks it help a lot

Unknown said...

Can you do it and still leave some spaces?

javin paul said...

Yes, just remove the matcher for space like s+ in above example

Post a Comment