String.GetEnumerator 方法

定义

检索一个可以循环访问此字符串中的每个字符的对象。

public:
 CharEnumerator ^ GetEnumerator();
public CharEnumerator GetEnumerator ();
member this.GetEnumerator : unit -> CharEnumerator
Public Function GetEnumerator () As CharEnumerator

返回

CharEnumerator

枚举器对象。

示例

以下示例将多次访问多个字符串中的字符,并显示有关各个字符的信息。 它使用语言迭代构造,而不是对 方法 GetEnumerator 的调用。

using namespace System;

void EnumerateAndDisplay(String^ phrase)
{
   Console::WriteLine("The characters in the string \"{0}\" are:", 
                      phrase);

   int CharCount = 0;
   int controlChars = 0;
   int alphanumeric = 0;
   int punctuation = 0;

   for each (Char ch in phrase) {
      Console::Write("'{0}' ", (! Char::IsControl(ch)) ? ch.ToString() : 
                                "0x" + Convert::ToUInt16(ch).ToString("X4"));
      if (Char::IsLetterOrDigit(ch)) 
         alphanumeric++;
      else if (Char::IsControl(ch)) 
         controlChars++;
      else if (Char::IsPunctuation(ch)) 
         punctuation++;             
      CharCount++;
   }

   Console::WriteLine("\n   Total characters:        {0,3}", CharCount);
   Console::WriteLine("   Alphanumeric characters: {0,3}", alphanumeric);
   Console::WriteLine("   Punctuation characters:  {0,3}", punctuation);
   Console::WriteLine("   Control Characters:      {0,3}\n", controlChars);
}

int main()
{
   EnumerateAndDisplay("Test Case");
   EnumerateAndDisplay("This is a sentence.");
   EnumerateAndDisplay("Has\ttwo\ttabs");
   EnumerateAndDisplay("Two\nnew\nlines");
}
// The example displays the following output:
//    The characters in the string "Test Case" are:
//    'T' 'e' 's' 't' ' ' 'C' 'a' 's' 'e'
//       Total characters:          9
//       Alphanumeric characters:   8
//       Punctuation characters:    0
//       Control Characters:        0
//    
//    The characters in the string "This is a sentence." are:
//    'T' 'h' 'i' 's' ' ' 'i' 's' ' ' 'a' ' ' 's' 'e' 'n' 't' 'e' 'n' 'c' 'e' '.'
//       Total characters:         19
//       Alphanumeric characters:  15
//       Punctuation characters:    1
//       Control Characters:        0
//    
//    The characters in the string "Has       two     tabs" are:
//    'H' 'a' 's' '0x0009' 't' 'w' 'o' '0x0009' 't' 'a' 'b' 's'
//       Total characters:         12
//       Alphanumeric characters:  10
//       Punctuation characters:    0
//       Control Characters:        2
//    
//    The characters in the string "Two
//    new
//    lines" are:
//    'T' 'w' 'o' '0x000A' 'n' 'e' 'w' '0x000A' 'l' 'i' 'n' 'e' 's'
//       Total characters:         13
//       Alphanumeric characters:  11
//       Punctuation characters:    0
//       Control Characters:        2
using System;

class Example
{
   public static void Main() 
   {
      EnumerateAndDisplay("Test Case");
      EnumerateAndDisplay("This is a sentence.");
      EnumerateAndDisplay("Has\ttwo\ttabs" );
      EnumerateAndDisplay("Two\nnew\nlines");
   }

   static void EnumerateAndDisplay(String phrase)
   {
      Console.WriteLine("The characters in the string \"{0}\" are:",
                        phrase);

      int CharCount = 0;
      int controlChars = 0;
      int alphanumeric = 0;
      int punctuation = 0;

      foreach (var ch in phrase) {
         Console.Write(Char.IsControl(ch) ? $"{ch}" : $"0x{(ushort)ch:X4}");

         if (Char.IsLetterOrDigit(ch)) 
            alphanumeric++;
         else if (Char.IsControl(ch)) 
            controlChars++;
         else if (Char.IsPunctuation(ch)) 
            punctuation++;             
         CharCount++;
      }

      Console.WriteLine("\n   Total characters:        {0,3}", CharCount);
      Console.WriteLine("   Alphanumeric characters: {0,3}", alphanumeric);
      Console.WriteLine("   Punctuation characters:  {0,3}", punctuation);
      Console.WriteLine("   Control Characters:      {0,3}\n", controlChars);
   }
}
// The example displays the following output:
//    The characters in the string "Test Case" are:
//    'T' 'e' 's' 't' ' ' 'C' 'a' 's' 'e'
//       Total characters:          9
//       Alphanumeric characters:   8
//       Punctuation characters:    0
//       Control Characters:        0
//    
//    The characters in the string "This is a sentence." are:
//    'T' 'h' 'i' 's' ' ' 'i' 's' ' ' 'a' ' ' 's' 'e' 'n' 't' 'e' 'n' 'c' 'e' '.'
//       Total characters:         19
//       Alphanumeric characters:  15
//       Punctuation characters:    1
//       Control Characters:        0
//    
//    The characters in the string "Has       two     tabs" are:
//    'H' 'a' 's' '0x0009' 't' 'w' 'o' '0x0009' 't' 'a' 'b' 's'
//       Total characters:         12
//       Alphanumeric characters:  10
//       Punctuation characters:    0
//       Control Characters:        2
//    
//    The characters in the string "Two
//    new
//    lines" are:
//    'T' 'w' 'o' '0x000A' 'n' 'e' 'w' '0x000A' 'l' 'i' 'n' 'e' 's'
//       Total characters:         13
//       Alphanumeric characters:  11
//       Punctuation characters:    0
//       Control Characters:        2
Module Example
   Sub Main()
      EnumerateAndDisplay("Test Case")
      EnumerateAndDisplay("This is a sentence.")
      EnumerateAndDisplay("Has" & vbTab & "two" & vbTab & "tabs")
      EnumerateAndDisplay("Two" & vbLf & "new" & vbLf & "lines")
   End Sub 
       
   Sub EnumerateAndDisplay(phrase As String)
      Console.WriteLine("The characters in the string ""{0}"" are:", phrase)
       
      Dim charCount As Integer = 0
      Dim controlChars As Integer = 0
      Dim alphanumeric As Integer = 0
      Dim punctuation As Integer = 0
        
      For Each ch In phrase          
         Console.Write("'{0}' ", If(Not Char.IsControl(ch), ch, 
                                     "0x" + Convert.ToUInt16(ch).ToString("X4")))
         If Char.IsLetterOrDigit(ch) Then 
            alphanumeric += 1
         Else If Char.IsControl(ch) Then 
            controlChars += 1
         Else If Char.IsPunctuation(ch) Then 
            punctuation += 1             
         End If
         CharCount += 1
      Next

      Console.WriteLine()
      Console.WriteLine("   Total characters:        {0,3}", CharCount)
      Console.WriteLine("   Alphanumeric characters: {0,3}", alphanumeric)
      Console.WriteLine("   Punctuation characters:  {0,3}", punctuation)
      Console.WriteLine("   Control Characters:      {0,3}", controlChars)
      Console.WriteLine()
    End Sub 
End Module 
' This example displays the following output:
'    The characters in the string "Test Case" are:
'    'T' 'e' 's' 't' ' ' 'C' 'a' 's' 'e'
'       Total characters:          9
'       Alphanumeric characters:   8
'       Punctuation characters:    0
'       Control Characters:        0
'    
'    The characters in the string "This is a sentence." are:
'       'T' 'h' 'i' 's' ' ' 'i' 's' ' ' 'a' ' ' 's' 'e' 'n' 't' 'e' 'n' 'c' 'e' '.'
'          Total characters:         19
'          Alphanumeric characters:  15
'          Punctuation characters:    1
'          Control Characters:        0
'       
'       The characters in the string "Has       two     tabs" are:
'       'H' 'a' 's' '0x0009' 't' 'w' 'o' '0x0009' 't' 'a' 'b' 's'
'          Total characters:         12
'          Alphanumeric characters:  10
'          Punctuation characters:    0
'          Control Characters:        2
'       
'       The characters in the string "Two
'       new
'       lines" are:
'       'T' 'w' 'o' '0x000A' 'n' 'e' 'w' '0x000A' 'l' 'i' 'n' 'e' 's'
'          Total characters:         13
'          Alphanumeric characters:  11
'          Punctuation characters:    0
'          Control Characters:        2

注解

提示

应改为在 GetEnumerator C#、C++/CLR 和 Visual Basic) 中使用语言迭代构造 (,而不是调用 方法来检索随后用于 CharEnumerator 枚举字符串的 对象。 在 C# 中为 foreach,在 C++/CLR 中为 foreach,在 C# 中为for each Visual Basic) 。

此方法使你能够对字符串中的单个字符进行一次访问。 例如,Visual Basic和 C# 语句调用此方法以返回一个 对象,该对象可提供对此字符串实例中的字符 For Each foreach CharEnumerator 的只读访问权限。

适用于

另请参阅