我想做的很简单。用户输入一个字符串。该程序检查并计算该文本字符串中存在的元音数量。
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
int counter = 0;
string s = txtInput.Text.Trim();
char[] arr = s.ToCharArray();
foreach (char i in arr)
{
//checks the 2 arrays for matches
counter++;
}
我如何检查 2 个数组以查看是否有任何匹配项?
谢谢。
最佳答案
arr.Any(p => vowels.Contains(p));
更新: 计算您可以使用的匹配数;
int count = arr.Where(p => vowels.Contains(p)).Count();
关于C# - 比较 2 个数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9972563/