» » How to check if a character is a special character in java

  • To check if string contains special character or not or  check whether first character is special character or not we can use regex or contains method of sting class.
  • By using  java.util.regex.Matcher class and java.util.regex.Pattern class methods we can check whether string has special character or not
  • Lets see an example program on how to check a string has special character or not using java regex.



Program #1: Java example program to check string has special character or not using  java regex's?
 
  1. package StringInterviewprograms;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class CheckSpecialCharacterString {
  6.  
  7.     /**
  8.      * Check whether the String contains special character or not using java
  9.      * @author www.instanceofjava.com
  10.      */
  11.     
  12. public static void main(String[] args) {
  13.  
  14. String Str="Java String interview questions ";
  15. String Str1="Java String interview questions % ";
  16. Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
  17. Matcher m = p.matcher(Str);
  18. boolean check = m.find();
  19.  
  20. if (check)
  21.    System.out.println("String: "+Str+" contains special character");
  22. else
  23.    System.out.println("String: "+Str+" does not contains any special character");
  24.  
  25. Matcher m1= p.matcher(Str1);
  26.  
  27. boolean flag = m1.find();
  28.  
  29. if(flag)
  30.        System.out.println("String: "+Str1+" contains special character");
  31.     else
  32.        System.out.println("String: "+Str1+" does not contains any special character");
  33.  
  34.    }
  35. }
Output:

  1. String: Java String interview questions  does not contains any special character
  2. String: Java String interview questions %  contains special character

  • We can check each character of a string is special character or not without using java regex's.
  • By using String class contains method and for loop we can check each character of a sting is special character or not.
  • Let us see a java example program on how to check each character (including space) of a string is special character or not.
  • String validation for special characters.


Program #2: Java example program to check each character of string is special character or not without using  java regex's?

  1. package StringInterviewprograms;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class CheckSpecialCharacterString {
  7.  
  8.     /**
  9.      * Check whether the each character of String is special character or not using java
  10.      * @author www.instanceofjava.com
  11.      */
  12.     
  13. public static void main(String[] args) {
  14. String Str="Java String interview questions*$%";
  15.   
  16. String specialCharacters=" !#$%&'()*+,-./:;<=>?@[]^_`{|}";
  17.  
  18. for (int i = 0; i < Str.length(); i++) {
  19.     
  20.     if (specialCharacters.contains(Character.toString(Str.charAt(i))))
  21.     {
  22.        
  23.         System.out.println(Str.charAt(i)+": is a special character");
  24.     }  
  25.   }
  26.  
  27. }
  28.  
  29. }

Output:


  1.  : is a special character
  2.  : is a special character
  3.  : is a special character
  4. *: is a special character
  5. $: is a special character
  6. %: is a special character

Program #3: Java example program to check each character of string is special character or not without using  java regex's Using Eclipse IDE.


Check Special Character String java

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu