(1) 单个字符的查询
foreach
(2) 字符串的查询
RegEx
示例代码:
static void CountStrings()
{
string test = "Hello, how are youyou you you you, you you yy?";
int wordCount = 0;
int charCount = 0;
foreach (Match m in Regex.Matches(test, "you"))
{
wordCount++;
}
foreach (char value in test)
{
if (value == 'y')
{
charCount++;
}
}
Console.WriteLine("Word count: {0}", wordCount);
Console.WriteLine("Character count: {0}", charCount);
}