java.lang.Character isWhitespace(char ch)

Description

The Character.isWhitespace(char ch) java method determines if the specified character is white space according to Java. A character is a Java whitespace character if and only if it satisfies one of the following criteria:

  • It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space (‘u00A0’, ‘u2007’, ‘u202F’).
  • It is ‘t’, U+0009 HORIZONTAL TABULATION.
  • It is ‘n’, U+000A LINE FEED.
  • It is ‘u000B’, U+000B VERTICAL TABULATION.
  • It is ‘f’, U+000C FORM FEED.
  • It is ‘r’, U+000D CARRIAGE RETURN.
  • It is ‘u001C’, U+001C FILE SEPARATOR.
  • It is ‘u001D’, U+001D GROUP SEPARATOR.
  • It is ‘u001E’, U+001E RECORD SEPARATOR.
  • It is ‘u001F’, U+001F UNIT SEPARATOR.

This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the isWhitespace(int) method.

The isWhitespace(char ch) method of Character class is static thus it should be accessed statically which means the we would be calling this method in this format:

Character.isWhitespace(char ch)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java isWhitespace() method non statically.

Method Syntax

public static boolean isWhitespace(char ch)

Method Argument

Data Type Parameter Description
char ch the character to be tested.

Method Returns

The isValidCodePoint(int codePoint) method of Character class returns true if the specified code point value is between MIN_CODE_POINT and MAX_CODE_POINT inclusive; false otherwise.

Compatibility

Requires Java 1.1 and up

Java Character isWhitespace(char ch) Example

Below is a simple java example on the usage of isWhitespace(char ch) method of Character class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of 
 * isWhitespace(char ch) method of Character class.
 */

public class CharacterIsWhiteSpaceCharExample {

	public static void main(String[] args) {

		// Ask for user input
		System.out.print("Enter an input:");

		// use scanner to get the user input
		Scanner s = new Scanner(System.in);

		// gets the user input
		char[] value = s.nextLine().toCharArray();

		// close the scanner object
		s.close();

		// check if user input is white space
		for (char ch : value) {
			boolean result = Character.isWhitespace(ch);
			// print the result
			System.out.println("character " + ch 
					+ " is a white space? " + result);
		}

	}

}

Sample Output

Below is the sample output when you run the above example.

Java Character isWhitespace(char ch)