Sdílet prostřednictvím


Indexování řetězců

Třída System.Globalization.StringInfo poskytuje metody, které vám umožňují rozdělit řetězec na textové prvky a iterovat přes tyto textové prvky. Textový prvek je jednotka textu, která je zobrazena jako jeden znak nazvaný grafém. Textový prvek může být znak bez diakritického znaménka, náhradní pár nebo sekvence znaků s diakritikou. Další informace o náhradních párech a sekvencích znaků s diakritikou naleznete v tématu Unicode Support for Surrogate Pairs and Combining Character Sequences.

Použijte metodu StringInfo.GetTextElementEnumerator pro vytvoření enumerátoru, který může iterovat přes prvky řetězce. Použijte metodu StringInfo.ParseCombiningCharacters pro vrácení indexů každého základního znaku bez diakritického znaménka, vysokého náhradního nebo řídícího znaku v specifikovaném řetězci.

V následujícím příkladu kódu je vytvořen řetězec arabských znaků obsahující sekvence znaků s diakritikou. V strCombining, například Unicode kód U+0625 představuje arabský znak bez diakritického znaménka (Arabské písmeno Alef s Hamzou níže) a Unicode kód U+0650 představuje arabský znak diakritického znaménka (Arabská Kasra). Společně tyto kódy představují sekvenci znaků s diakritikou a proto musí být analyzovány jako jeden textový prvek. Dále je vytvořen řetězec obsahující náhradní páry. V strSurrogates, například Unicode kód U+DACE představuje vysokého náhradníka a Unicode kód U+DEFF představuje nízkého náhradníka. Dohromady tyto kódy představují náhradní pár a musí být analyzovány jako jeden textový prvek. Každý řetězec je analyzován jednou pomocí metody ParseCombiningCharacters a znovu pomocí metody GetTextElementEnumerator . Obě metody správně analyzují textové prvky v strCombining na indexech, 0, 2, 3, 5 a 6. Obě metody správně analyzují textové prvky v strSurrogates na indexech, 0, 2, 4, 5 a 6. Výsledky operací analýzy jsou zobrazeny.

Imports System
Imports System.IO
Imports System.Globalization
Imports System.Text
Imports Microsoft.VisualBasic

Public Class StringInfoSample

   Public Shared Sub Main()
      ' Creates a string with text elements at <0;2;3;5;6>.
      ' The Unicode code points specify Arabic 
      ' combining character sequences.
      Dim strCombining As String = ChrW(&H625) & ChrW(&H650) & _
         ChrW(&H64A) & ChrW(&H647) & ChrW(&H64E) & ChrW(&H627) & _
         ChrW(&H628) & ChrW(&H64C)

      ' Creates a string with text elements at <0;2;4;5;6>.
      'The Unicode code points specify private surrogate pairs.
      Dim strSurrogates As String = ChrW(&HDACE) & ChrW(&HDEFF) & _
         ChrW(&HDAAF) & ChrW(&HDEFC) & "a" & ChrW(&HD8BF) & ChrW(&HDD99)
      
      EnumerateTextElements(strCombining)
      EnumerateTextElements(strSurrogates)
   End Sub

   Public Shared Sub EnumerateTextElements(str As String)
      ' Creates a TextElementEnumerator.
      Dim TEIndices As Integer() = Nothing
      Dim TEEnum As TextElementEnumerator = Nothing      

      ' Parses the string using the ParseCombiningCharacters() method.
      Console.WriteLine(ControlChars.Newline + "Parsing '{0}' Using _
         ParseCombiningCharacters()...", str)
      Dim i As Integer
      TEIndices = StringInfo.ParseCombiningCharacters(str)
      For i = 0 To (TEIndices.Length - 1) - 1
         Console.WriteLine("Text Element {0} ({1}..{2})= {3}", i, _
            TEIndices(i), TEIndices((i + 1)) - 1, _
            str.Substring(TEIndices(i), TEIndices((i + 1)) - _
            TEIndices(i)))
      Next i
      Console.WriteLine("Text Element {0} ({1}..{2})= {3}", i, _
         TEIndices(i), str.Length - 1, str.Substring(TEIndices(i)))

      ' Parses the string using the GetTextElementEnumerator method.
      Console.WriteLine(ControlChars.Newline + "Parsing '{0}' Using _
         TextElementEnumerator...", str)
      TEEnum = StringInfo.GetTextElementEnumerator(str)

      Dim Continue As Boolean = False
      Dim TECount As Integer = - 1

      ' Note: Begins at element -1 (none).
      Continue = TEEnum.MoveNext()
      While Continue
         ' Prints the current element.
         ' Both GetTextElement() and Current retrieve the current
         ' text element. The latter returns it as an Object.
         TECount += 1
         Console.WriteLine("Text Element {0} ({1}..{2})= {3}", TECount, _
            TEEnum.ElementIndex, TEEnum.ElementIndex + _
            TEEnum.GetTextElement().Length - 1, TEEnum.Current)

         ' Moves to the next element.
         Continue = TEEnum.MoveNext()
      End While
   End Sub
End Class
using System;
using System.IO;
using System.Globalization;
using System.Text;

public class StringInfoSample
{
   public static void Main()
   {
      // Creates a string with text elements at <0;2;3;5;6>.
      // The Unicode code points specify Arabic 
      // combining character sequences.
      string strCombining =   
            "\u0625\u0650\u064A\u0647\u064E\u0627\u0628\u064C";
      // Creates a string with text elements at <0;2;4;5;6>.
      // The Unicode code points specify private surrogate pairs.
      string strSurrogates = "\uDACE\uDEFF\uDAAF\uDEFCa\uD8BF\uDD99"; 

      EnumerateTextElements(strCombining);
      EnumerateTextElements(strSurrogates);
   }

   public static void EnumerateTextElements(string str)
   {
      // Creates a TextElementEnumerator.
      int[] TEIndices = null;
      TextElementEnumerator TEEnum = null;

      // Parses the string using the ParseCombiningCharacters() method.
      Console.WriteLine
         ("\r\nParsing '{0}' Using ParseCombiningCharacters()...",str);
      int i;
      TEIndices = StringInfo.ParseCombiningCharacters(str);
      for (i = 0; i < (TEIndices.Length - 1); i++)
      {
         Console.WriteLine
            ("Text Element {0} ({1}..{2})= 
            {3}",i,TEIndices[i],TEIndices[i+1] - 1,
            str.Substring(TEIndices[i],TEIndices[i+1] - TEIndices[i]));
      }
      Console.WriteLine
         ("Text Element {0} ({1}..{2})= {3}",i,TEIndices[i],str.Length - 
         1, str.Substring(TEIndices[i]));

      // Parses the string using the GetTextElementEnumerator method.
      Console.WriteLine
         ("\r\nParsing '{0}' Using TextElementEnumerator...",str);
      TEEnum = StringInfo.GetTextElementEnumerator(str);

      bool Continue = false;
      int TECount = -1;

      // Note: Begins at element -1 (none).
      Continue = TEEnum.MoveNext();
      while (Continue)
      {
         // Prints the current element.
         // Both GetTextElement() and Current retrieve the current
         // text element. The latter returns it as an Object.
         TECount++;
         Console.WriteLine("Text Element {0} ({1}..{2})=  
               {3}",TECount,TEEnum.ElementIndex,
               TEEnum.ElementIndex + TEEnum.GetTextElement().Length - 1, 
               TEEnum.Current);

         // Moves to the next element.
         Continue = TEEnum.MoveNext();
         }
   }
}
PoznámkaPoznámka

Pokud spustíte tento kód v aplikaci konzoly, tak zadané textové prvky Unicode se nezobrazí správně, protože prostředí konzoly nepodporuje všechny Unicode znaky.

Viz také

Odkaz

StringInfo

Koncepty

Standard Unicode v rozhraní .NET Framework

Další zdroje

Kódování a lokalizace