Identifying Whitespace Characters with isWhitespace() Method in Java: A Comprehensive Guide
☰Fullscreen
Table of Content:
Description
The method determines whether the specified char value is a white space, which includes space, tab, or new line.
Method Syntax
public static boolean isWhitespace(char ch)
Method Argument
Data Type | Parameter | Description |
---|---|---|
char | ch | the character to be tested. Primitive character type. |
Method Returns
This method returns true, if the passed character is really a white space.
Compatibility
Requires Java 1.1 and up
Example
Below is a simple java example on the usage of isWhitespace(char ch) method of Character class.
public class MethodisWhitespace { public static void main(String args[]) { System.out.println(Character.isWhitespace('d')); System.out.println(Character.isWhitespace(' ')); System.out.println(Character.isWhitespace('\n')); System.out.println(Character.isWhitespace('\t')); } }
output
Below is the sample output when you run the above example.
false true true true Press any key to continue . . .
Example
Below is a simple java example on the usage of isWhitespace(char ch) method of Character class.
import java.util.Scanner; /* * This example source code demonstrates the use of * isWhitespace(char ch) method of Character class. */ public class CharacterIsWhiteSpaceChar { 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); } } }
output
Below is the sample output when you run the above example.
Enter an input:s character s is a white space? false Press any key to continue . . . Enter an input: character is a white space? true Press any key to continue . . .