【发布时间】:2018-11-29 18:51:17
【问题描述】:
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++;
}
【问题讨论】:
-
所以基本上你想“遍历每个 uniquechar 序列”并检查
if(line.Contains(one of the uniquechars))然后 => 计数你的计数器......抱歉奇怪的伪代码,但这应该让你开始跨度> -
另一个提示。您应该在集合中收集您的独特字符,而不是字符串
-
谢谢这真的很简单,我现在明白了。
-
Thanks to a comment I figured it out in a simple way. If anyone needs, here's how I got it...请把你的答案放在答案部分(下面)——而不是你的问题中。 -
请将您的解决方案转移到自己的答案中,谢谢。