Char.IsHighSurrogate(String, Int32) Method in C#

The Char.IsHighSurrogate(String, Int32) method in C# determines whether the character at the specified position in a string is a high surrogate. High surrogates are Unicode code units in the range U+D800 to U+DBFF, used as the first part of surrogate pairs to represent characters beyond the Basic Multilingual Plane.

Syntax

Following is the syntax −

public static bool IsHighSurrogate(string str, int index);

Parameters

  • str − The string to evaluate.

  • index − The position of the character to evaluate in the string.

Return Value

Returns true if the character at the specified position is a high surrogate (U+D800 to U+DBFF); otherwise, false. If the index is out of range, an ArgumentOutOfRangeException is thrown.

Unicode Surrogate Pairs High Surrogate U+D800 - U+DBFF Low Surrogate U+DC00 - U+DFFF Forms Combined to represent characters beyond BMP (Basic Multilingual Plane)

Using IsHighSurrogate with High Surrogate Character

This example checks if a high surrogate character at index 4 is detected −

using System;

public class Demo {
    public static void Main(){
        string str = new String(new char[] { 'k', 'm', 'g', 't', '\uD800' });
        bool res = Char.IsHighSurrogate(str, 4);
        if (res)
            Console.WriteLine("Contains High Surrogate value!");
        else
            Console.WriteLine("Does not contain High Surrogate value!");
    }
}

The output of the above code is −

Contains High Surrogate value!

Using IsHighSurrogate with Regular Character

This example checks if a regular character at index 2 is detected as a high surrogate −

using System;

public class Demo {
    public static void Main(){
        string str = new String(new char[] { 'k', 'm', 'g', 't', '\uD800' });
        bool res = Char.IsHighSurrogate(str, 2);
        if (res)
            Console.WriteLine("Contains High Surrogate value!");
        else
            Console.WriteLine("Does not contain High Surrogate value!");
    }
}

The output of the above code is −

Does not contain High Surrogate value!

Checking Multiple Positions

This example demonstrates checking multiple positions in a string containing various Unicode characters −

using System;

public class Demo {
    public static void Main(){
        string str = "A\uD800\uDC00B\uD834\uDD1EC";
        Console.WriteLine("String: " + str);
        Console.WriteLine("Length: " + str.Length);
        
        for (int i = 0; i < str.Length; i++) {
            bool isHighSurrogate = Char.IsHighSurrogate(str, i);
            Console.WriteLine($"Index {i}: '{str[i]}' (U+{(int)str[i]:X4}) - High Surrogate: {isHighSurrogate}");
        }
    }
}

The output of the above code is −

String: A?B?C
Length: 7
Index 0: 'A' (U+0041) - High Surrogate: False
Index 1: '?' (U+D800) - High Surrogate: True
Index 2: '?' (U+DC00) - High Surrogate: False
Index 3: 'B' (U+0042) - High Surrogate: False
Index 4: '?' (U+D834) - High Surrogate: True
Index 5: '?' (U+DD1E) - High Surrogate: False
Index 6: 'C' (U+0043) - High Surrogate: False

Conclusion

The Char.IsHighSurrogate(String, Int32) method is essential for Unicode text processing, helping identify high surrogate characters that form the first part of surrogate pairs. This is particularly useful when working with characters beyond the Basic Multilingual Plane in UTF-16 encoded strings.

Updated on: 2026-03-17T07:04:36+05:30

219 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements