In Java, Character.isSpaceChar(ch) returns true when the character's general category is SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR. It is the pure Unicode idea of a space.
So it accepts the ordinary space, the non-breaking space U+00A0, the em space U+2003, the ideographic space U+3000, and U+2028/U+2029, but it rejects tab, newline, and carriage return, because Unicode files those as control characters. That is the opposite trade-off to isWhitespace().
A static method of java.lang.Character (Java 1.1+): call it on the class, as in Character.isSpaceChar(...); 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.isSpaceChar(' '); // true
Character.isSpaceChar('\u00A0'); // true, non-breaking space
Character.isSpaceChar('\t'); // false, tab is a control character
Character.isSpaceChar('\n'); // false
Overloads: isSpaceChar(char ch) and isSpaceChar(int codePoint). The older Character.isSpace(char) is deprecated; use this method or isWhitespace instead.
Library declarations
Java
// java.lang.Character
public static boolean isSpaceChar(char ch)
public static boolean isSpaceChar(int codePoint) // since Java 5
Parameters
Parameter
Description
ch (char)
The character to test.
codePoint (int)
The Unicode code point to test.
Return Value
Return Value
Description
true
The category is SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR.
false
Anything else, including tab, newline, and the zero width space U+200B (a format character).
Both agree on the space. U+0020 (32), the em space (8195), and LINE SEPARATOR U+2028 (8232) pass both tests.
Controls split them. Tab and newline are control characters, so only isWhitespace accepts them.
The non-breaking space goes the other way. U+00A0 (160) is a Unicode space separator, so only isSpaceChar accepts it.
Example 2 — Mark Every Kind of Space in a String
Replace Unicode spaces with _ and the remaining whitespace (here a tab) with ~.
Java
public class CharacterIsSpaceCharMark {
public static void main(String[] args) {
String text = "a\u00A0b\u2003c\td e";
StringBuilder shown = new StringBuilder();
for (char c : text.toCharArray()) {
if (Character.isSpaceChar(c)) {
shown.append('_');
} else if (Character.isWhitespace(c)) {
shown.append('~');
} else {
shown.append(c);
}
}
System.out.println(shown);
}
}
Output
a_b_c~d_e
How It Works
Three Unicode spaces. The non-breaking space, the em space, and the normal space before e are all SPACE_SEPARATOR, so each turns into _.
The tab falls through.'\t' fails isSpaceChar but passes isWhitespace in the else if, so it becomes ~.
Readable debugging. The output is plain ASCII, so you can see exactly where the invisible characters were.
Example 3 — isSpaceChar and the Three Separator Categories
Print the category name of four characters next to the isSpaceChar result.
Java
public class CharacterIsSpaceCharCategory {
static String category(int type) {
if (type == Character.SPACE_SEPARATOR) return "SPACE_SEPARATOR";
if (type == Character.LINE_SEPARATOR) return "LINE_SEPARATOR";
if (type == Character.PARAGRAPH_SEPARATOR) return "PARAGRAPH_SEPARATOR";
if (type == Character.FORMAT) return "FORMAT";
return "other (" + type + ")";
}
public static void main(String[] args) {
char[] chars = {' ', '\u3000', '\u2029', '\u200B'};
for (char c : chars) {
System.out.println((int) c + " " + category(Character.getType(c))
+ " spaceChar=" + Character.isSpaceChar(c));
}
}
}
Constants, not magic numbers. The helper compares getType with Character.SPACE_SEPARATOR and friends, which reads better than 12, 13, and 14.
Wide and paragraph spaces. The ideographic space U+3000 (12288) used in CJK text is a space separator, and U+2029 (8233) is the paragraph separator; both pass.
Zero width is not a space. U+200B ZERO WIDTH SPACE has the category FORMAT despite its name, so isSpaceChar returns false.
Frequently Asked Questions
No. Tab, newline, and carriage return are control characters in Unicode, so isSpaceChar returns false. isWhitespace returns true for them.
Character.isSpaceChar(c) && !Character.isWhitespace(c) is true for U+00A0, U+2007, and U+202F. Or compare with '\u00A0' directly.
That getType returns SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR.
No. Its category is FORMAT, so isSpaceChar is false. isIdentifierIgnorable returns true for it.
isWhitespace for Java-style whitespace and isSpaceChar for Unicode spaces. isSpace only recognised five ASCII characters.