字符串中某个字符出现的次数
有一个字符串"aabbce"
找出a。b的总共出现次数
写法1
public static int GetVowelCount(string str)
{
return str.Count(i => "ab".Contains(i));
}
写法2
public static int GetVowelCount(string str)
{
int vowelCount = 0;
for (int i = 0; i < str.Length; i++)
{
if (str[i] == 'a'||str[i] == 'b')
{
vowelCount++;
}
}
return vowelCount;
}
比较推荐写法1