C# 我想数一数“的数目;";C语言中字符串中的字符#

C# 我想数一数“的数目;";C语言中字符串中的字符#,c#,C#,每次我运行程序时,它都会因为以下代码行而不断崩溃: if (s[i] == 'e') 有人能帮我吗 Console.WriteLine("Enter a string:"); string s = Console.ReadLine(); Console.WriteLine(s); int anz = 0; for (int i = 0; i <= s.Length; i++) { if (s[i] == 'e')

每次我运行程序时,它都会因为以下代码行而不断崩溃:

if (s[i] == 'e')
有人能帮我吗

    Console.WriteLine("Enter a string:");
    string s = Console.ReadLine();
    Console.WriteLine(s);
    int anz = 0;

    for (int i = 0; i <= s.Length; i++)
    {
        if (s[i] == 'e')
            anz++;
        else if (s[i] == 'E')
        {
            anz++;
        }
    }

    Console.WriteLine("There are" + anz + "e's in your string! ");
    Console.ReadKey();
Console.WriteLine(“输入字符串:”);
字符串s=Console.ReadLine();
控制台。写入线(s);
int anz=0;
对于(inti=0;i变化

for (int i = 0; i <= s.Length; i++)
for(inti=0;ichange

for (int i = 0; i <= s.Length; i++)

for(int i=0;i您可以使用Linq的
Count()
并省略所有循环和变量声明:

int anz = s.Count(i => i == 'e' || i == 'E');

您可以使用Linq的
Count()
并省略所有循环和变量声明:

int anz = s.Count(i => i == 'e' || i == 'E');

您可以这样尝试:

string search = "search the E character";
int count = 0;
foreach (var character in search.ToLower())
{
    if (character.Equals('e'))
        count += 1;
}

您可以这样尝试:

string search = "search the E character";
int count = 0;
foreach (var character in search.ToLower())
{
    if (character.Equals('e'))
        count += 1;
}

我知道,但是为什么这个不能用呢?我想用这里的这些命令来做。问题出在你的循环中。你得到了什么错误?想想你迭代了多少项。你需要先把字符串转换成数组,然后循环数组。
char[]array=s.ToCharArray()
谢谢大家谢谢你我设法解决了它!祝你愉快!我知道,但为什么它不能与这个一起工作?我想用这里的这些命令来做更改。问题出在你的循环中。你得到了什么错误?想想你迭代了多少项。你需要先将字符串转换为数组,然后循环数组。
char[]array=s.ToCharArray();
感谢所有人感谢你我成功地解决了它!祝你度过愉快的一天!