字符串索引

System.Globalization.StringInfo 类提供了一些方法,可用于将字符串拆分为文本元素,并循环访问这些文本元素。 文本元素是显示为单字符的文本单元,称作字素。 文本元素可以是基字符、代理项对或组合字符序列。 有关代理项的更多信息匹配,并且组合字符序列,请参见 Char 结构主题的“字符对象、Unicode字符和字符串”一节。

使用 StringInfo.GetTextElementEnumerator 方法可以创建可循环访问字符串元素的枚举数。 使用 StringInfo.ParseCombiningCharacters 方法可返回指定字符串中每一个基字符、高代理项或控制字符的索引。

在下面的代码示例中,将创建一个包含组合字符序列的阿拉伯字符串。 例如,在 strCombining 中,Unicode 代码 U+0625 表示一个阿拉伯基字符(后面带有 Hamza 的阿拉伯文字母 Alef),Unicode 代码 U+0650 表示一个阿拉伯组合字符(阿拉伯文 Kasra)。 这两个代码合在一起表示一个组合字符序列,因此必须被分析为单个文本元素。 接下来,创建一个包含代理项对的字符串。 例如,在 strSurrogates 中,Unicode 代码 U+DACE 表示一个高代理项,Unicode 代码 U+DEFF 表示一个低代理项。 这两个代码合在一起表示一个代理项对,并且必须被分析为单个文本元素。 每一个字符串用 ParseCombiningCharacters 方法分析一次,再用 GetTextElementEnumerator 方法分析一次。 这两个方法都将正确分析 strCombining 中位于索引 0、2、3、5 和 6 处的文本元素。 这两个方法都将正确分析 strSurrogates 中位于索引 0、2、4、5 和 6 处的文本元素。 将显示分析操作的结果。

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();
         }
   }
}

备注

如果在控制台应用程序中执行此代码,则指定的 Unicode 文本元素将不会正确显示,因为控制台环境并不支持所有的 Unicode 字符。

请参见

参考

StringInfo

其他资源

对 .NET Framework 应用程序进行全球化和本地化