本文整理汇总了C#中System.String.GetEnumerator方法的典型用法代码示例。如果您正苦于以下问题:C# String.GetEnumerator方法的具体用法?C# String.GetEnumerator怎么用?C# String.GetEnumerator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.String
的用法示例。
在下文中一共展示了String.GetEnumerator方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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("'{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);
}
}
输出:
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