Java - Character isWhitespace() Method



The Java Character isWhitespace() method determines if the specified input character is a white space according to Java.

Any of the following characters can be considered as the whitespace characters −

  • Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but not also a non-breaking space ('\u00A0', '\u2007', '\u202F').

  • '\t', U+0009 − HORIZONTAL TABULATION.

  • '\n', U+000A − LINE FEED.

  • '\u000B', U+000B − VERTICAL TABULATION.

  • '\f', U+000C − FORM FEED.

  • '\r', U+000D − CARRIAGE RETURN.

  • '\u001C', U+001C − FILE SEPARATOR.

  • '\u001D', U+001D − GROUP SEPARATOR.

  • '\u001E', U+001E − RECORD SEPARATOR.

  • '\u001F', U+001F − UNIT SEPARATOR.

This method occurs in two polymorphic forms with different parameters but same return type.

Syntax

Following is the syntax for Java Character isWhitespace() method

public static boolean isWhitespace(char ch)
(or)
public static boolean isWhitespace(int codePoint)

Parameters

  • ch − the character to be tested

  • codePoint − the Unicode code point to be tested

Return Value

This method returns true if the character is a Java whitespace character, otherwise it returns false.

Example

The following example shows the usage of Java Character isWhitespace(char ch) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 char primitives ch1, ch2
      char ch1, ch2;

      // assign values to ch1, ch2
      ch1 = ' ';
      ch2 = '\f';

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      /**
       *  check if ch1, ch2 are whitespace characters and 
       *  assign results to b1, b2
       */
      b1 = Character.isWhitespace(ch1);
      b2 = Character.isWhitespace(ch2);
      String str1 = "ch1 is a Java whitespace character is " + b1;
      String str2 = "ch2 is a Java whitespace character is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ch1 is a Java whitespace character is true
ch2 is a Java whitespace character is true

Example

The following example shows the usage of Java Character isWhitespace(int codePoint) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 int primitives cp1, cp2
      int cp1, cp2;

      // assign values to cp1, cp2
      cp1 = 0x001c; // represents FILE SEPARATOR
      cp2 = 0x0abc;

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      /**
       *  check if cp1, cp2 represent whitespace characters 
       *  and assign results to b1, b2
       */
      b1 = Character.isWhitespace(cp1);
      b2 = Character.isWhitespace(cp2);
      String str1 = "cp1 represents Java whitespace character is " + b1;
      String str2 = "cp2 represents Java whitespace character is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

cp1 represents Java whitespace character is true
cp2 represents Java whitespace character is false

Example

Another example to show how to use the method in a program is given below. We use line feed and tab space as the arguments and check the return value.

import java.lang.*;
public class WhitespaceDemo {
   public static void main(String args[]) {
      char ch = '\n';
      int cp = 0x0009;
      boolean b1 = Character.isWhitespace(ch);
      boolean b2 = Character.isWhitespace(cp);
      System.out.println(ch + " is a Whitespace: " + b1);
      System.out.println((char)cp + " is a Whitespace: " + b2);
   }
}

Output

Let us compile and run the above program, and the output will be printed as follows −

is a Whitespace: true
is a Whitespace: true

Example

This method can also be called by passing each element of a character array to individually check whether they are whitespace characters. This is possible using loop statements. Then, the method is used as a condition to the conditional statements.

import java.lang.*;
public class WhitespaceDemo {
   public static void main(String args[]) {
      char ch[] = {'\u001C', '\u001D', '\u025E', '\u001F'};
      for(int i = 0; i < ch.length; i++) {
         if(Character.isWhitespace(ch[i]))
            System.out.println(ch[i] + " is a white space");
         else
            System.out.println(ch[i] + " is not a white space");
      }
   }
}

Output

The output of the given program above, after compiling and executing, is displayed as follows −

is a white space
 is a white space
ɞ is not a white space
 is a white space
java_lang_character.htm
Advertisements