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


The Char.IsSurrogatePair() method in C# is used to indicate whether two adjacent Char objects at a specified position in a string form a surrogate pair.

Syntax

Following is the syntax −

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

Above, the string str is a string, whereas the index is the starting position of the pair of characters to evaluate within str.

Example

Let us now see an example to implement the Char.IsSurrogatePair() method −

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

Output

This will produce the following output −

Does not contain Surrogate pair!

Example

Let us now see another example −

using System;
public class Demo {
   public static void Main(){
      string str = new String(new char[] { 'k','l','a', '\uD800', '\uDC00' });
      bool res = Char.IsSurrogatePair(str, 3);
      if (res)
         Console.WriteLine("Contains Surrogate pair!");
      else
         Console.WriteLine("Does not contain Surrogate pair!");
   }
}

Output

This will produce the following output −

Contains Surrogate pair!

Updated on: 13-Nov-2019

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements