Java Programing laungage

java.util Projects

java.util Project 1

Escaping Special Characters

This tutorials illustrates us how to escape all special characters from the given string or text. Here, we can learn the way of detecting all special characters present in the given string.

Previous Home Next

Program Description

In this page of the programming tutorials we are going to take an example for the best explanation about the way of matching special characters in the given string or text. This example finds special characters and replaces all the detected special characters by "F" character and display whole string with the "F" character place of any special characters in the given string or text.

Code Description

Pattern.compile("[^a-zA-Z0-9 ]")

This method is used to compile a regular expression "[^a-zA-Z0-9 ]" which does not detects a to z, A to Z and 0 to 9 characters, that means it will be detect all special symbols or characters.


package r4r;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class specialcharacter {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(;
System.out.print("Enter string to find special characters: ");
String str = br.readLine();
Pattern pattern = Pattern.compile("[^a-zA-Z0-9]");
Matcher matcher = pattern.matcher(str);
String str1 = matcher.replaceAll("F");

Previous Home Next