Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check whether the specified character has a surrogate code in C#
In C#, a surrogate is a character that represents a Unicode code point outside the Basic Multilingual Plane (BMP). Surrogate characters are used to encode Unicode characters that require more than 16 bits, using a pair of 16-bit values called high surrogate and low surrogate.
The Char.IsSurrogate() method checks whether a character at a specified position in a string is a surrogate character.
Syntax
Following is the syntax for checking surrogate characters −
bool result = Char.IsSurrogate(string, index); bool result = Char.IsSurrogate(char);
Parameters
-
string − The string containing the character to check
-
index − The position of the character in the string
-
char − The character to check directly
Return Value
Returns true if the character is a surrogate (either high or low surrogate), otherwise returns false.
Using Char.IsSurrogate() with String
Example
using System;
public class Demo {
public static void Main() {
string str = new String(new char[] { 'k', 'm', 'g', 't', '\uD800' });
bool res = Char.IsSurrogate(str, 4);
if (res)
Console.WriteLine("Contains Surrogate value!");
else
Console.WriteLine("Does not contain Surrogate value!");
// Check what type of surrogate it is
if (Char.IsHighSurrogate(str, 4))
Console.WriteLine("It's a high surrogate");
else if (Char.IsLowSurrogate(str, 4))
Console.WriteLine("It's a low surrogate");
}
}
The output of the above code is −
Contains Surrogate value! It's a high surrogate
Using Char.IsSurrogate() with Regular Characters
Example
using System;
public class Demo {
public static void Main() {
string str = new String(new char[] { 'k', 'm', 'g', 't', 'w' });
bool res = Char.IsSurrogate(str, 4);
if (res)
Console.WriteLine("Contains Surrogate value!");
else
Console.WriteLine("Does not contain Surrogate value!");
// Check individual character
char ch = 'A';
Console.WriteLine($"Character '{ch}' is surrogate: {Char.IsSurrogate(ch)}");
}
}
The output of the above code is −
Does not contain Surrogate value! Character 'A' is surrogate: False
Working with Surrogate Pairs
Example
using System;
public class Demo {
public static void Main() {
// Creating a string with emoji (requires surrogate pair)
string emoji = "?"; // Smiling face emoji
Console.WriteLine($"String: {emoji}");
Console.WriteLine($"Length: {emoji.Length}"); // Will be 2, not 1
// Check each character in the emoji
for (int i = 0; i < emoji.Length; i++) {
char ch = emoji[i];
Console.WriteLine($"Position {i}: '{ch}' (U+{((int)ch):X4}) - IsSurrogate: {Char.IsSurrogate(ch)}");
if (Char.IsHighSurrogate(ch))
Console.WriteLine(" -> High surrogate");
else if (Char.IsLowSurrogate(ch))
Console.WriteLine(" -> Low surrogate");
}
}
}
The output of the above code is −
String: ? Length: 2 Position 0: '?' (U+D83D) - IsSurrogate: True -> High surrogate Position 1: '?' (U+DE00) - IsSurrogate: True -> Low surrogate
Conclusion
The Char.IsSurrogate() method is essential for working with Unicode characters beyond the Basic Multilingual Plane. It helps identify surrogate characters that are part of surrogate pairs used to represent emojis, mathematical symbols, and other extended Unicode characters in UTF-16 encoding.
