Sunday, September 19, 2021

How to remove all special characters from String in Java? Example Tutorial

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 marks 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 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 that separates the average programmers from 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]", "");
System.out.println(text);

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

}

Output
Thistexthasallotofspecialcharacters
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.


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 objects 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)
Thanks for reading this Java Regular Expression tutorial so far. If you find this Java Regex tutorial and example useful please share it with your friends and colleagues. 

8 comments:

  1. Just in case your language uses more than the few ASCII characters from a to z, Unicode properties are pretty useful: http://regular-expressions.mobi/unicode.html

    ReplyDelete
  2. Thanks man, simple yet effective solution.

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

    ReplyDelete
  4. Can you do it and still leave some spaces?

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

    ReplyDelete