string uniquechars = "a,e,i"
int counting = 0;
foreach (var item in line)
{
if (chars.IndexOf(item) != -1)
{
counting++;
}
}
此代码查找特定字符在字符串中的次数,但是我不太了解如何使每个字母仅计数一次。例如,aaaaeeeeiiii
是这一行,我希望输出为Number of unique letters appeared that appeared: 3
对于3个字符,我可以创建一个if,但是如果我有大约8个唯一字符并且它们也区分大小写,那将是无效的。
感谢评论,我以一种简单的方式弄清楚了。这是我的方法:
char[] seperators = { ' ', '.', ',', '!', '?', ':', ';', '(', ')', '\t' };
string chars = "A;E,I.Y;O;U;a;e;i;y;o;u;Ą;ą;Ę;ę;Ė;ė;Į;į;Ų;ų;Ū;ū";
string[] Uniquechars = chars.Split(seperators);
int counting = 0;
for (int i = 0; i < Uniquechars.Length; i++)
{
if (eil.Contains(Uniquechars[i])) counting++;
}
答案 0 :(得分:0)
您可以只计算不同的字符:
var count = chars.Distinct().Count(char.IsLetter);