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

The Char.IsHighSurrogate() Method is useful while working with Unicode characters, specifically in handling the surrogate pairs. This Method indicates whether the Char object at the specified position in a string is a high surrogate or not within a string, which is vital for accurate Unicode character handling.

Surrogate pairs:

Unicode has an extensive character set and encounters limitations in representing characters beyond the basic multilingual plane BLP. To solve this issue, Unicode employs surrogate pairs. A surrogate pair comprises two 16-bit code units. A high surrogate and a low surrogate will form a unique code point to represent characters beyond the BMP.

High surrogates are the first part of a surrogate pair. Low surrogates are the second part of a surrogate pair. High surrogate ranges are between the U+D800 and U+DBFF, whereas the small surrogate ranges are between U+DC00 and U+DFFF.

Char.IsHighSurrogate() method:

This method is essential when working with text that may contain characters represented by surrogate pairs. This identification is useful in accurate processing of Unicode characters. When manipulating strings that involve surrogate pairs, developers need to distinguish between regular characters and high surrogates. This method allows developers to apply specific logic for handling the surrogate pairs.

Syntax:

The syntax of the Char.IsHighSurrogate(char c) method.

Here, the 'c' is a Unicode character of char datatype that is used to identify whether it is a regular character or a high surrogate. This method will return a Boolean value. It returns true if the specified character is a high surrogate; otherwise, it returns false.

The syntax of the Char.IsHighSurrogate(char c, int index) method.

Here, the 's' is a string that is used to check for a high surrogate character. The index is the position of the character in the string. This method returns a Boolean value. Where true represents the specified index in the string is a high surrogate character; otherwise, it returns false.

Example:

Let us take a C# program to illustrate the Char.IsHighSurrogate(char c, int index) method.

Output:

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

Explanation:

This program uses Char.IsHighSurrogate method to check each character in a string and determine whether it is a high surrogate or not. There is a variable named customString, which represents a random string that contains characters 'a' and 'b' and an emoji. The program uses a for loop to iterate through each character in the customString. After that, the Char.IsHighSurrogate method takes two parameters: string and the character's index to be checked. The program prints the information about each character, including its index, character itself and whether it is a high surrogate or not.

Example:

Let us take an example program for handling surrogate pairs in C#.

Output:

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

Explanation:

This program demonstrates the usage of the Char.IsHighSurrogate Method to determine whether a character at a specified index in a string is a high surrogate. The program includes various examples, checking for high surrogates in different strings and scenarios. It utilizes exception handling to catch potential issues, such as a negative index. The daily life example illustrates the practical application of identifying emoji characters within a string, as emojis often involve surrogate pairs. The program showcases how developers can leverage the IsHighSurrogate method to handle and process surrogate pairs effectively in real-world scenarios.