In Java, Character.isSurrogate(ch) returns true when the char is a surrogate of either kind: high (0xD800 to 0xDBFF) or low (0xDC00 to 0xDFFF). The whole range is 0xD800 to 0xDFFF.
It answers a simple question: "is this char part of a character above U+FFFF?" If no char in a string is a surrogate, every char is a whole character, and char-by-char code is safe.
A static method of java.lang.Character (Java 7+): call it on the class, as in Character.isSurrogate(...); you do not need a Character object. No import is needed, because java.lang is always available.
c >= Character.MIN_SURROGATE && c <= Character.MAX_SURROGATE
The range check written out.
One overload, isSurrogate(char ch), added in Java 7. The range constants Character.MIN_SURROGATE and Character.MAX_SURROGATE exist since Java 5.
Library declarations
Java
// java.lang.Character (since Java 7)
public static boolean isSurrogate(char ch)
public static final char MIN_SURROGATE = '\uD800';
public static final char MAX_SURROGATE = '\uDFFF';
An int loop for all chars. Looping with an int from 0 to 0xFFFF and casting avoids the endless loop a char counter would cause, because a char is never greater than 0xFFFF.
Two old-style checks. "high or low" and "between MIN_SURROGATE and MAX_SURROGATE" both work on Java 5 and 6.
Full agreement. All 65536 chars give the same answer three ways, and exactly 2048 of them are surrogates.
Frequently Asked Questions
Java 7. On older versions use isHighSurrogate(c) || isLowSurrogate(c).
0xD800 to 0xDFFF inclusive, which covers both high and low surrogates.
No. It looks at one char only. Use isSurrogatePair(high, low) to check two chars in order.
Surrogates are a char-level detail of UTF-16. For code points, use isBmpCodePoint or isSupplementaryCodePoint.
2048: 1024 high surrogates and 1024 low surrogates, enough to encode the 1,048,576 supplementary code points.