Pin It

How to check if a character is a whitespace or not in Java ?.

Program to demonstrate how to check if a character is a whitespace or not in Java

package com.hubberspot.example;


public class IsWhiteSpaceTest {

   public static void main(String[] args) {
        // Create a String of character having letters, digits 
   // spaces and symbols in between just for a test
 String characters = "Q 1 w 2 E 5 y $ % a - ^ ";

 // we take the length of characters by length()
 int length = characters.length();

 // we than loop each character one by one 
 for(int i = 0; i < length; i++) {
            // we store each character in a variable
     // for each loop
     char character = characters.charAt(i);
            
     // checking for each character that its a whitespace character
     // or not by calling Character class isWhitespace() method , 
     // by passing the character. It returns true if the character 
     // passed is whitespace character or not
     if(Character.isWhitespace(character)) {
      System.out.println("Given character : '" + characters.charAt(i)
    + "' is a whitespace character");
     }
     else {
  System.out.println("Given character : '" + characters.charAt(i)
    + "' is not a whitespace character");
     }
 }

  }


}


Output of the program : 
 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot