Char.IsLowSurrogate Method (String, Int32)

May 11, 2014

Indicates whether the Char object at the specified position in a string is a low surrogate.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static bool IsLowSurrogate(
	string s,
	int index
)

Parameters

s
Type: System.String
A string.
index
Type: System.Int32
The position of the character to evaluate in s.

Return Value

Type: System.Boolean
true if the numeric value of the specified character in the s parameter ranges from U+DC00 through U+DFFF; otherwise, false.

ExceptionCondition
ArgumentNullException

s is null.

ArgumentOutOfRangeException

index is not a position within s.

The index parameter is zero-based.

In addition to representing single characters using a 16-bit code unit, UTF-16 encoding allows abstract characters to be represented using two 16-bit code units, which is known as a surrogate pair. The second element in this pair is the low surrogate. Its code unit can range from U+DC00 to U+DFFF. An individual surrogate has no interpretation of its own; it is meaningful only when used as part of a surrogate pair.

The following code example demonstrates the IsHighSurrogate, IsLowSurrogate, and IsSurrogatePair methods.


// This example demonstrates the Char.IsSurrogatePair() method
using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      char cHigh = '\uD800';
      char cLow = '\uDC00';
      string s1 = new String(new char[] { 'a', '\uD800', '\uDC00', 'z' });
      string divider = String.Concat("\n", new String('-', 70),
                                      "\n");

      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("Hexadecimal code point of the character, cHigh: {0:X4}", (int)cHigh) + "\n";
      outputBlock.Text += String.Format("Hexadecimal code point of the character, cLow:  {0:X4}", (int)cLow) + "\n";
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("Characters in string, s1: 'a', high surrogate, low surrogate, 'z'") + "\n";
      outputBlock.Text += String.Format("Hexadecimal code points of the characters in string, s1: ") + "\n";
      for (int i = 0; i < s1.Length; i++)
      {
         outputBlock.Text += String.Format("s1[{0}] = {1:X4} ", i, (int)s1[i]) + "\n";
      }
      outputBlock.Text += divider + "\n";

      outputBlock.Text += "Is each of the following pairs of characters a surrogate pair?" + "\n";
      outputBlock.Text += String.Format("C1) cHigh and cLow?  - {0}", Char.IsSurrogatePair(cHigh, cLow)) + "\n";
      outputBlock.Text += String.Format("C2) s1[0] and s1[1]? - {0}", Char.IsSurrogatePair(s1, 0)) + "\n";
      outputBlock.Text += String.Format("C3) s1[1] and s1[2]? - {0}", Char.IsSurrogatePair(s1, 1)) + "\n";
      outputBlock.Text += String.Format("C4) s1[2] and s1[3]? - {0}", Char.IsSurrogatePair(s1, 2)) + "\n";
      outputBlock.Text += divider + "\n";
   }
}
/*
This example produces the following results:

Hexadecimal code point of the character, cHigh: D800
Hexadecimal code point of the character, cLow:  DC00

Characters in string, s1: 'a', high surrogate, low surrogate, 'z'
Hexadecimal code points of the characters in string, s1:
s1[0] = 0061
s1[1] = D800
s1[2] = DC00
s1[3] = 007A

----------------------------------------------------------------------

Is each of the following pairs of characters a surrogate pair?
C1) cHigh and cLow?  - True
C2) s1[0] and s1[1]? - False
C3) s1[1] and s1[2]? - True
C4) s1[2] and s1[3]? - False

----------------------------------------------------------------------
*/


Windows Phone OS

Supported in: 8.1, 8.0

Show:
© 2014 Microsoft