gpt4 book ai didi

C# - 比较 2 个数组中的值

转载 作者:太空宇宙 更新时间:2023-11-03 17:21:45 26 4
gpt4 key购买 nike

我想做的很简单。用户输入一个字符串。该程序检查并计算该文本字符串中存在的元音数量。

    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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com