Thursday, February 9, 2017

How to remove all special characters from String in Java

You can use 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 character 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 couple fo examples to remove all special characters from String in Java.

Java Program to remove all special characters from String

Here is our simple 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 average programmer to good programmer. 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 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

Post a Comment