Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

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 : 
 
 
© 2021 Learn Java by Examples Template by Hubberspot