Java Character isWhitespace() method Example

Many methods exist in java.lang.Character class used for different purposes. Some methods are useful for checking the character is a digit or letter or is a uppercase letter or lowercase letter etc. At the sametime, some methods can modify the case of the letters from uppercase to lowercase or vice versa.

Few more methods exist to check whether the character is fit to be a whitae space character or not like the present one isWhitespace(char ch) and the previous one isSpace(char ch).

Note: isWhitespace(char ch) is not deprecated but earlier method isSpace(char ch) is deprecated.

Following is the method signature as defined in java.lang.Character class.

  • public static boolean isWhitespace(char ch): Checks the specified character ch is a white space character or not. Returns a boolean value of true for space characters like \t, \n, \f, \r and ' ' (space in between single quotes).

Following example illustrates.

public class CharacterFunctionDemo
{
  public static void main(String args[])
  {
   char ch1 = ' ';
   char ch2 = '\t';                      

   boolean b1 = Character.isWhitespace(ch1);
   boolean b2 = Character.isWhitespace(ch2);

   System.out.println("' ' is a white space character: " + b1);     
   System.out.println("\\t is a white space character: " + b2);     

   System.out.println("\\n is a white space character: " + Character.isWhitespace('\n'));
   System.out.println("\\f is a white space character : " + Character.isWhitespace('\f'));
   System.out.println("\\r is a white space character: " + Character.isWhitespace('\r'));
  }
}


Java Character isWhitespace() method ExampleOutput screenshot on Java Character isWhitespace() method Example

There comes a similar method with the same functionality – isSpace(char).

Leave a Comment

Your email address will not be published.