Replace Special Characters In Java

Replace Special Characters In Java | The characters which are not numeric and alphabetic are termed as special characters except the whitespace characters. Example:- “[email protected]#$%^&*()” these characters are called special characters. In this post, we will see Java replaceall special characters. Also see:- Java Program to Find Weight of String

These special characters are sometimes unwanted in the string or we may have typed them by mistake. Hence to remove or replace these special characters we can take the help of replace() or replaceAll() method of the Java String class. These are built-in methods in Java that allows us to remove the species characters or character sequence from the given string. Also see:- Java replace() vs replaceAll() Method

Let us see the example to understand more. In the below example, special characters are replaced with ‘X’ characters.

String word = "Spec$$ial Characters";
System.out.println(word.replaceAll("[[email protected]#$%^&]", "X"));

Output:-

SpecXXial Characters

Replace Special Characters In Java using replaceAll()

The syntax of the replaceAll() method is following:- public String replaceAll(String regex, String replacement)

Parameter:- regex – the regular expression to which this string is to be matched; replacement – the string to be substituted for each match.
Return:- The resulting String.
Throws:- PatternSyntaxException – if the regular expression’s syntax is invalid.

Java replace special characters by using replaceAll()

public class Main {
   public static void main(String[] args) {
      String str = "[email protected]#$Programming Language";
      System.out.println(str.replaceAll("[@#$%^&*]", "K"));
   }
}

Output:-

JavaKKKProgramming Language

Java remove special characters by using replaceAll()

If we want to remove a special character from the given string then we have to pass an empty string as the second parameter to the replaceAll() method.

public class Main {
   public static void main(String[] args) {
      String str = "[email protected]#$Programming Language";
      System.out.println(str.replaceAll("[@#$%^&*]", ""));
   }
}

Output:-

JavaProgramming Language

See another example to demonstrate replaceAll()

public class Main {
   public static void main(String[] args) {
      String str = "Know ##Pr%ogram*#2025";
      System.out.println("Actual String = " + str);

      str = str.replaceAll("[^a-zA-Z0-9]", "");
      System.out.println("String after replacement = " + str);
   }
}

Output:

Actual String = Know ##Pr%ogram*#2025
String after replacement = KnowProgram2025

In the above example, except ‘a’ to ‘z’, ‘A’, to ‘Z’ & ‘0’ to ‘9], all other characters are removed from the string.

Java remove Special Characters without using replaceAll() method

public class Main {
   public static void main(String[] args) {
      String string = "[email protected]#a Pr^ogram$$min&g";
      for (int i = 0; i < string.length(); i++) {
         char ch = string.charAt(i);
         if ((ch < 'A' || ch > 'Z') && (ch < 'a' || ch > 'z') 
               && (ch < '0' || ch > '9')) {
            string = string.substring(0, i) + string.substring(i + 1);
            i--;
         }
      }
      System.out.print(string);
   }
}

Output:-

JavaProgramming

In the above example, we have iterated through the string character by character & checked whether the given number is a special number or not. If the character is an alphabet or number then it is not a special number, else it is a special character and it should be removed from the given string.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *