Condividi tramite


Char.IsSurrogatePair Method (String, Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Indicates whether two adjacent Char objects at a specified position in a string form a surrogate pair.

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

Syntax

'Declaration
Public Shared Function IsSurrogatePair ( _
    s As String, _
    index As Integer _
) As Boolean
public static bool IsSurrogatePair(
    string s,
    int index
)

Parameters

Return Value

Type: System.Boolean
true if the s parameter and the index parameter specify a pair of adjacent characters, and the numeric value of the character at position index ranges from U+D800 through U+DBFF, and the numeric value of the character at position index+1 ranges from U+DC00 through U+DFFF; otherwise, false.

Exceptions

Exception Condition
ArgumentNullException

s is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

index is not a position within s.

Remarks

Ordinarily, a single character is represented by a single 16-bit Unicode code point. UTF-16 encoding also supports surrogate pairs, which allow a single abstract character to be represented by two 16-bit code points. The first code point, whose value can range from U+D800 to U+DBFF, is the high surrogate. The second code point, whose value can range from U+DC00 to U+DFFF, is the low surrogate. Individual surrogate code points have no interpretation of their own. For more information about surrogates and the Unicode Standard, see the Unicode home page.

Examples

The following example demonstrates the IsSurrogatePair method.

' This example demonstrates the Char.IsSurrogatePair() method

Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim cHigh As Char = ChrW(&HD800)
      Dim cLow As Char = ChrW(&HDC00)
      Dim s1 = New [String](New Char() {"a"c, ChrW(&HD800), ChrW(&HDC00), "z"c})
      Dim divider As String = [String].Concat(vbCrLf, _
                                              New [String]("-"c, 70), _
                                              vbCrLf)

      outputBlock.Text &= vbCrLf
      outputBlock.Text &= String.Format("Hexadecimal code point of the character, cHigh: {0:X4}", AscW(cHigh)) & vbCrLf
      outputBlock.Text &= String.Format("Hexadecimal code point of the character, cLow:  {0:X4}", AscW(cLow)) & vbCrLf
      outputBlock.Text &= vbCrLf
      outputBlock.Text &= String.Format("Characters in string, s1: 'a', high surrogate, low surrogate, 'z'") & vbCrLf
      outputBlock.Text &= String.Format("Hexadecimal code points of the characters in string, s1: ") & vbCrLf
      Dim i As Integer
      For i = 0 To s1.Length - 1
         outputBlock.Text &= String.Format("s1({0}) = {1:X4} ", i, AscW(s1.Chars(i))) & vbCrLf
      Next i
      outputBlock.Text &= divider & vbCrLf

      outputBlock.Text &= "Is each of the following pairs of characters a surrogate pair?" & vbCrLf
      outputBlock.Text &= String.Format("C1) cHigh and cLow?  - {0}", [Char].IsSurrogatePair(cHigh, cLow)) & vbCrLf
      outputBlock.Text &= String.Format("C2) s1(0) and s1(1)? - {0}", [Char].IsSurrogatePair(s1, 0)) & vbCrLf
      outputBlock.Text &= String.Format("C3) s1(1) and s1(2)? - {0}", [Char].IsSurrogatePair(s1, 1)) & vbCrLf
      outputBlock.Text &= String.Format("C4) s1(2) and s1(3)? - {0}", [Char].IsSurrogatePair(s1, 2)) & vbCrLf
      outputBlock.Text &= divider & vbCrLf
   End Sub 'Main
End Class 'Sample
'
'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
'
'----------------------------------------------------------------------
// 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

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.