文字列インデックス付け

System.Globalization.StringInfo クラスには、1 つの文字列をテキスト要素に分割して、テキスト要素を反復処理するためのメソッドが含まれています。テキスト要素とは、書記素と呼ばれる、1 文字として表示されるテキスト単位です。基本文字、サロゲート ペア、または組み合わせ文字シーケンスをテキスト要素として使用できます。サロゲート ペアと組み合わせ文字シーケンスの詳細については、「サロゲート ペアと組み合わせ文字シーケンスの Unicode サポート」を参照してください。

文字列の要素を反復処理する列挙子を作成するには、StringInfo.GetTextElementEnumerator メソッドを使用します。指定された文字列の各基本文字、上位サロゲート、または制御文字のインデックスを返すには、StringInfo.ParseCombiningCharacters メソッドを使用します。

組み合わせ文字シーケンスが含まれているアラビア語文字の文字列を作成するコード例を次に示します。たとえば strCombining では、Unicode コード U+0625 がアラビア語の基本文字 (ハムザが付いたアラビア文字「アーレフ」) を表し、Unicode コード U+0650 がアラビア語の組み合わせ文字 (アラビア文字「カスラ」) を表します。この 2 つのコードの組み合わせにより、1 つの組み合わせ文字シーケンスが表されるため、この 2 つのコードは 1 つのテキスト要素として解析される必要があります。次に、サロゲート ペアが含まれている文字列が作成されます。たとえば strSurrogates では、Unicode コード U+DACE が上位サロゲートを表し、Unicode コード U+DEFF が下位サロゲートを表します。この 2 つのコードの組み合わせにより、1 つのサロゲート ペアが表されるため、この 2 つのコードは 1 つのテキスト要素として解析される必要があります。各文字列は、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();
         }
   }
}
Noteメモ :

コンソール環境では一部の Unicode 文字がサポートされていないため、コンソール アプリケーションでこのコードを実行しても、指定された Unicode テキスト要素が正しく表示されません。

参照

関連項目

StringInfo Class

概念

.NET Framework での Unicode

その他の技術情報

エンコーディングとローカリゼーション