The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to check if a character is a special character in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
How to check if a character is a special character in java Posted: May 6, 2017 3:48 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: How to check if a character is a special character in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • 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

Read: How to check if a character is a special character in java

Topic: Java Tutorial: Enum in java | Java enum - Level Previous Topic   Next Topic Topic: Java Tutorial: Enum in java[How to apply enum on switch statement - Day]

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use