String.GetEnumerator 方法的C#例子



下面的代码示例使用 GetEnumerator 方法显示输入字符串中的每个 System.Char。

view plaincopy to clipboardprint
?
// Example for the String.GetEnumerator( ) method.   
using System;   
using System.Collections;   
  
class GetEnumerator    
{   
    
public static void Main()    
    
{   
        Console.WriteLine(    
            
"This example of String.GetEnumerator( ) " +   
            
"generates the following output." );   
  
        EnumerateAndDisplay( 
"Test Case" );   
        EnumerateAndDisplay( 
"Has\ttwo\ttabs" );   
        EnumerateAndDisplay( 
"Two\nnew\nlines" );   
    }
   
  
    
static void EnumerateAndDisplay( String Operand )   
    
{   
        Console.WriteLine(    
            
"\nThe characters in the string \"{0}\" are:",   
            Operand );   
  
        IEnumerator OperandEnum 
= Operand.GetEnumerator( );   
        
int         CharCount = 0;   
  
        
while( OperandEnum.MoveNext( ) )   
        
{   
            CharCount
++;   
            Console.Write( 
" '{0}' ", OperandEnum.Current );   
        }
   
        Console.WriteLine( 
"\n Character count: {0}", CharCount );   
    }
   
}
   
  
/*  
This example of String.GetEnumerator( ) generates the following output.  
 
The characters in the string "Test Case" are:  
 'T'  'e'  's'  't'  ' '  'C'  'a'  's'  'e'  
 Character count: 9  
 
The characters in the string "Has       two     tabs" are:  
 'H'  'a'  's'  '       '  't'  'w'  'o'  '     '  't'  'a'  'b'  's'  
 Character count: 12  
 
The characters in the string "Two  
new  
lines" are:  
 'T'  'w'  'o'  '  
'  'n'  'e'  'w'  '  
'  'l'  'i'  'n'  'e'  's'  
 Character count: 13  
posted @ 2007-08-11 14:02  过河卒A  阅读(1985)  评论(0编辑  收藏  举报