Home Java Character Methods isSurrogatePair() Java Character isSurrogatePair() Method Definition In Java, Character.isSurrogatePair(high, low) returns true when high is a high surrogate and low is a low surrogate. Only then do the two chars encode one supplementary character, which toCodePoint() can decode.
The order matters: a low surrogate followed by a high surrogate is not a pair. Two high surrogates in a row, or a surrogate next to an ordinary char, are not pairs either.
A static method of java.lang.Character (Java 5+): call it on the class, as in Character.isSurrogatePair(...); you do not need a Character object. No import is needed, because java.lang is always available.
Syntax Pass the two chars in the order they appear in the text.
Character.isSurrogatePair('\uD83D', '\uDE00'); // true, U+1F600
Character.isSurrogatePair('\uDE00', '\uD83D'); // false, wrong order
Character.isSurrogatePair('\uD83D', 'A'); // false
Character.isSurrogatePair('a', 'b'); // false Call What it does Character.isSurrogatePair(s.charAt(i), s.charAt(i + 1))Does a valid pair start at i? if (Character.isSurrogatePair(h, l)) cp = Character.toCodePoint(h, l);Decode only when it is safe. Character.isHighSurrogate(h) && Character.isLowSurrogate(l)What the method checks.
One overload, isSurrogatePair(char high, char low), since Java 5.
Library declarations // java.lang.Character (since Java 5)
public static boolean isSurrogatePair(char high, char low) Parameters Parameter Description high (char)The first char; must be a high surrogate for a true result. low (char)The second char; must be a low surrogate.
Return Value Return Value Description trueisHighSurrogate(high) && isLowSurrogate(low).falseAny other combination.
Compare with Similar Methods Related surrogate checks:
Java Examples 1. Pair or not 2. Well-formed text 3. Decode only valid pairs Example 1 — Valid Pairs, Reversed Pairs, and Non-Pairs Try the emoji's two halves in both orders, a doubled high surrogate, and two letters.
public class CharacterIsSurrogatePairTable {
public static void main(String[] args) {
char hi = '\uD83D';
char lo = '\uDE00';
System.out.println("(hi, lo) -> " + Character.isSurrogatePair(hi, lo));
System.out.println("(lo, hi) -> " + Character.isSurrogatePair(lo, hi));
System.out.println("(hi, hi) -> " + Character.isSurrogatePair(hi, hi));
System.out.println("(a, b) -> " + Character.isSurrogatePair('a', 'b'));
}
} (hi, lo) -> true
(lo, hi) -> false
(hi, hi) -> false
(a, b) -> false How It Works The correct order. High surrogate first, low surrogate second: this is how U+1F600 appears in a String.Reversed is invalid. The same two chars in the other order fail, because the first argument must be a high surrogate.Not even surrogates. Two high surrogates, or two letters, can never form a pair.Example 2 — Check That Every Surrogate in a String Is Paired A string is well-formed UTF-16 when every surrogate is part of a valid pair. Check three strings.
public class CharacterIsSurrogatePairWellFormed {
static boolean wellFormed(String s) {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isSurrogate(c)) {
if (i + 1 < s.length() && Character.isSurrogatePair(c, s.charAt(i + 1))) {
i++;
} else {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
System.out.println("ok + emoji: " + wellFormed("ok\uD83D\uDE00"));
System.out.println("cut emoji: " + wellFormed("bad\uD83D"));
System.out.println("stray low: " + wellFormed("\uDE00bad"));
}
} How It Works Only surrogates need checking. Ordinary chars are always fine; the loop only stops to look at chars that pass isSurrogate.Pair, then skip. A valid pair advances i past the low half; anything else ends the check with false.Two kinds of damage. A high surrogate at the very end and a low surrogate at the very start both make the string malformed.Example 3 — Decode a char Array, Keeping Invalid Halves As They Are Join valid pairs with toCodePoint; leave anything else as a single char value.
public class CharacterIsSurrogatePairDecode {
public static void main(String[] args) {
char[] data = {'\uD834', '\uDD1E', '\uDD1E', '\uD834'};
StringBuilder out = new StringBuilder();
for (int i = 0; i < data.length; i++) {
if (i + 1 < data.length && Character.isSurrogatePair(data[i], data[i + 1])) {
out.append(Character.toCodePoint(data[i], data[i + 1])).append(' ');
i++;
} else {
out.append((int) data[i]).append(' ');
}
}
System.out.println(out.toString().trim());
}
} How It Works A real pair first. 0xD834 followed by 0xDD1E is MUSICAL SYMBOL G CLEF, which decodes to 119070 (0x1D11E).The reverse order. The next two chars are 0xDD1E then 0xD834: low before high, so isSurrogatePair is false.Kept as single values. The two leftover halves are printed as 56606 and 55348, the way codePointAt reports unpaired surrogates.Frequently Asked Questions What does isSurrogatePair check? That the first char is a high surrogate (0xD800 to 0xDBFF) and the second is a low surrogate (0xDC00 to 0xDFFF).
Does the order of the arguments matter? Yes. isSurrogatePair(low, high) is false. In UTF-16 the high surrogate always comes first.
Why check before calling toCodePoint? toCodePoint does not validate its arguments, so an invalid pair gives a meaningless result instead of an error.
How can I tell if a String is well-formed UTF-16? Walk the chars and make sure every surrogate is part of an isSurrogatePair match. Java 8 has no built-in method for this.
Is there a code point version? No. Surrogate pairs only exist at the char level. Once decoded, a code point is checked with isSupplementaryCodePoint.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
15 people found this page helpful
Helpful Share Copy link Suggestion