Java Character isWhitespace() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Character

Definition

In Java, Character.isWhitespace(ch) returns true when the character is whitespace according to Java. That is the usual set of separators between words and lines: the space, '\t', '\n', '\r', form feed '\f', vertical tab U+000B, the four separator controls U+001C to U+001F, and Unicode space, line, and paragraph separators.

There is one deliberate exception: non-breaking spaces (U+00A0, U+2007, U+202F) are not whitespace here, because they are meant to glue words together. isSpaceChar() uses the pure Unicode definition and does include them.

A static method of java.lang.Character (Java 1.1+): call it on the class, as in Character.isWhitespace(...); you do not need a Character object. No import is needed, because java.lang is always available. The int codePoint overload was added in Java 5.

Unicode version.

The answer comes from the Unicode Character Database built into your JDK, and each Java release follows a specific Unicode version (Java 8, for example, uses Unicode 6.2). The characters used on this page have had the same properties for many Unicode versions, so the examples print the same result on any current JDK.

Syntax

Pass a char or an int code point.

Java
Character.isWhitespace(' ');        // true
Character.isWhitespace('\t');       // true
Character.isWhitespace('\n');       // true
Character.isWhitespace('\u00A0');   // false, non-breaking space
CallWhat it does
Character.isWhitespace(c)Is this a separator between words or lines?
while (i < n && Character.isWhitespace(s.charAt(i))) i++;Skip leading whitespace.
Character.isWhitespace(c) || Character.isSpaceChar(c)Also catch non-breaking spaces.

Overloads: isWhitespace(char ch) and isWhitespace(int codePoint). String.trim() uses a different, simpler rule: it removes every character up to U+0020.

Library declarations
Java
// java.lang.Character
public static boolean isWhitespace(char ch)
public static boolean isWhitespace(int codePoint)   // since Java 5

Parameters

ParameterDescription
ch (char)The character to test.
codePoint (int)The Unicode code point to test.

Return Value

Return ValueDescription
trueA space, tab, line break, form feed, U+000B, U+001C to U+001F, or a Unicode space/line/paragraph separator that is not non-breaking.
falseAnything else, including U+00A0, U+2007, and U+202F.

Compare with Similar Methods

Three different ideas of "space":

MethodHow it differs
isSpaceChar(ch)Unicode separators only: includes U+00A0, excludes '\t' and '\n'.
String.trim()Removes leading and trailing characters with codes up to U+0020.
isISOControl(ch)Control codes, including tab and newline, but not the space.

Java Examples

Example 1 — Spaces, Tabs, Line Breaks, and Two Unicode Spaces

Test the usual separators, the non-breaking space, the em space, and a letter.

Java
public class CharacterIsWhitespaceTable {
    public static void main(String[] args) {
        char[] chars = {' ', '\t', '\n', '\r', '\u000B', '\f', '\u00A0', '\u2003', 'x'};
        for (char c : chars) {
            System.out.println((int) c + " -> " + Character.isWhitespace(c));
        }
    }
}

How It Works

  1. Escape sequences for invisible characters. '\n' and '\r' are written as escape sequences; their \u forms would break the source code, because Java turns them into real line breaks before compiling.
  2. The classic six. Space (32), tab (9), newline (10), carriage return (13), vertical tab (11), and form feed (12) are all whitespace.
  3. Two Unicode spaces, two answers. The em space U+2003 (8195) is whitespace, but the non-breaking space U+00A0 (160) is excluded on purpose.

Example 2 — Count Words Separated by Any Whitespace

Walk the text once and start a new word after every run of whitespace, whatever mix of spaces, tabs, and line breaks it is.

Java
public class CharacterIsWhitespaceWords {
    public static void main(String[] args) {
        String text = "  one\ttwo\n three   four ";
        int words = 0;
        boolean inWord = false;
        for (int i = 0; i < text.length(); i++) {
            boolean ws = Character.isWhitespace(text.charAt(i));
            if (!ws && !inWord) {
                words++;
            }
            inWord = !ws;
        }
        System.out.println("words: " + words);
    }
}

How It Works

  1. Mixed separators. The text uses leading spaces, a tab, a newline followed by a space, three spaces in a row, and a trailing space.
  2. Count word starts. A word begins at a non-whitespace character that follows whitespace (or the start), which happens at o, t, t, and f.
  3. Runs collapse naturally. Extra spaces only keep inWord at false; they never add empty words, unlike split(" ").

Example 3 — Parse a Number Written with a Non-Breaking Space

Many locales write 12500 as "12 500" with U+00A0 in the gap. See which test notices it.

Java
public class CharacterIsWhitespaceNbsp {
    public static void main(String[] args) {
        String amount = "12\u00A0500";
        int ws = 0, sp = 0;
        StringBuilder digits = new StringBuilder();
        for (char c : amount.toCharArray()) {
            if (Character.isWhitespace(c)) ws++;
            if (Character.isSpaceChar(c)) sp++;
            if (!Character.isWhitespace(c) && !Character.isSpaceChar(c)) digits.append(c);
        }
        System.out.println("isWhitespace matches: " + ws);
        System.out.println("isSpaceChar matches: " + sp);
        System.out.println("as int: " + Integer.parseInt(digits.toString()));
    }
}

How It Works

  1. Invisible to isWhitespace. U+00A0 is a non-breaking space, which Java's whitespace rule excludes, so ws stays 0.
  2. Caught by isSpaceChar. The same character has the Unicode category SPACE_SEPARATOR, so sp becomes 1.
  3. Clean, then parse. Dropping characters that pass either test leaves "12500"; calling Integer.parseInt on the original string would have thrown NumberFormatException.

Frequently Asked Questions

Yes. Character.isWhitespace('\t') is true, along with space, newline, carriage return, form feed, and vertical tab.
By design: Java excludes U+00A0, U+2007, and U+202F because they are meant to prevent a line break. Use isSpaceChar to detect them.
isWhitespace includes control characters such as tab and newline but excludes non-breaking spaces. isSpaceChar follows Unicode categories: it includes non-breaking spaces but not tab or newline.
No. trim() removes characters whose code is U+0020 or lower. Java 11 added String.strip(), which uses Character.isWhitespace.
Loop over the characters and return false at the first one that is not whitespace. Java 11 added String.isBlank() for this.

Next: lowSurrogate()

Get the low (trailing) surrogate char of a supplementary code point (Java 7+).

lowSurrogate() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful