YouTip LogoYouTip

Character Iswhitespace

## Java Character.isWhitespace() Method The `Character.isWhitespace()` method is a built-in static method in Java's `Character` class. It is used to determine whether a specified character is a whitespace character according to Java criteria. In Java, whitespace is not limited to just the standard space bar character; it also includes tabs, line breaks, and other Unicode space separators. --- ## Syntax The `Character` class provides two overloaded versions of the `isWhitespace()` method: ### 1. Checking a `char` value (Basic Multilingual Plane) ```java public static boolean isWhitespace(char ch) ``` ### 2. Checking a Unicode code point (Supports supplementary characters) ```java public static boolean isWhitespace(int codePoint) ``` ### Parameters * **`ch`**: The character to be tested. * **`codePoint`**: The Unicode code point to be tested. ### Return Value * Returns `true` if the character or code point is a Java-defined whitespace character; otherwise, it returns `false`. --- ## What Defines a Whitespace Character in Java? A character is considered a Java whitespace character if and only if it satisfies one of the following criteria: 1. It is a Unicode space character (`SPACE_SEPARATOR`, `LINE_SEPARATOR`, or `PARAGRAPH_SEPARATOR`) but is **not** a non-breaking space (`'\u00A0'`, `'\u2007'`, `'\u202F'`). 2. It is `'\t'` (Horizontal Tabulation, `U+0009`). 3. It is `'\n'` (Line Feed, `U+000A`). 4. It is `'\u000B'` (Vertical Tabulation). 5. It is `'\f'` (Form Feed, `U+000C`). 6. It is `'\r'` (Carriage Return, `U+000D`). 7. It is `'\u001C'` (File Separator). 8. It is `'\u001D'` (Group Separator). 9. It is `'\u001E'` (Record Separator). 10. It is `'\u001F'` (Unit Separator). --- ## Code Examples ### Basic Usage Example The following example demonstrates how to use `Character.isWhitespace(char ch)` to check standard characters, spaces, newlines, and tabs. ```java public class Test { public static void main(String[] args) { // Check a standard letter System.out.println("Is 'c' whitespace? " + Character.isWhitespace('c')); // Check a standard space System.out.println("Is ' ' whitespace? " + Character.isWhitespace(' ')); // Check a newline character System.out.println("Is '\\n' whitespace? " + Character.isWhitespace('\n')); // Check a tab character System.out.println("Is '\\t' whitespace? " + Character.isWhitespace('\t')); } } ``` **Output:** ```text Is 'c' whitespace? false Is ' ' whitespace? true Is '\n' whitespace? true Is '\t' whitespace? true ``` --- ### Advanced Example: Filtering Whitespace from a String You can use `Character.isWhitespace()` to strip or count whitespace characters in a given string. ```java public class WhitespaceFilter { public static void main(String[] args) { String text = "Hello, \t World! \n Welcome to YouTip."; StringBuilder result = new StringBuilder(); int whitespaceCount = 0; for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if (Character.isWhitespace(ch)) { whitespaceCount++; } else { result.append(ch); } } System.out.println("Original Text: " + text.replace("\n", "\\n").replace("\t", "\\t")); System.out.println("Filtered Text: " + result.toString()); System.out.println("Total Whitespace Characters Found: " + whitespaceCount); } } ``` **Output:** ```text Original Text: Hello, \t World! \n Welcome to YouTip. Filtered Text: Hello,World!WelcometoYouTip. Total Whitespace Characters Found: 7 ``` --- ## Key Considerations * **Non-Breaking Spaces (NBSP):** The non-breaking space character (`'\u00A0'`) is **not** considered whitespace by `Character.isWhitespace()`. If you need to handle non-breaking spaces as whitespace, you may need to check for them explicitly or use alternative regex patterns like `\s`. * **Performance:** Using `Character.isWhitespace(char)` is highly optimized in the JVM and is much faster than using regular expressions (e.g., `String.matches("\\s")`) for single-character checks.
← Character TolowercaseEclipse Charset β†’