C# 列出所有的;A「';她在串

C# 列出所有的;A「';她在串,c#,string,C#,String,我想数一数一个巴黎弦中的所有“A” using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace TESTING { class Testing { static void Main(string[] args) {

我想数一数一个巴黎弦中的所有“A”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace TESTING
{
    class Testing
    {
        static void Main(string[] args)
        {
            //ask user for the filename
            string userInput = fetchFileName("Enter the textfile you want to view: ");

            //test if the filename writes anything to console
            string fileContents = File.ReadAllText(userInput);

            string theFileContents = analyseFile(fileContents);
         //   Console.WriteLine(theFileContents);
            Console.ReadLine();

       }

        private static string analyseFile(string fileContents)
        {
            string str = fileContents;
            if (str.Contains("A"))                
            {
               Console.WriteLine("YES");

            }
            else
            {
                Console.WriteLine("NO");
            }
            return str;
        }

        private static string fetchFileName(string askFileName)
        {
            Console.WriteLine(askFileName);
            string userAnswer = Console.ReadLine();
            return userAnswer;
        }
    }
}

最简单的方法之一是迭代文件中的所有字符,并检查字母是否等于所需的字母

当您意识到字符串只不过是一个字符数组时,可以执行以下操作:

public int LetterCount(string filename, char letter)
{
    int cnt = 0;
    string source = File.ReadAllText(filename);
    //Check every character in your string; if it matches increase the counter by 1
    foreach (char c in source)
    {
        if(c == letter)
        {
            cnt++;
        }
    }
    return cnt;
}
int A_count = LetterCount(@"C:\test.txt", 'A');
string text = "The quick brown fox jumps over the lazy dog";
int counter = 0;
for (int i = 0; i < text.Count(); i++)
{
    if (text[i] == 'a' || text[i] == 'A')
    {
        counter++;
    }
}
Console.WriteLine(counter);
然后像这样使用它:

public int LetterCount(string filename, char letter)
{
    int cnt = 0;
    string source = File.ReadAllText(filename);
    //Check every character in your string; if it matches increase the counter by 1
    foreach (char c in source)
    {
        if(c == letter)
        {
            cnt++;
        }
    }
    return cnt;
}
int A_count = LetterCount(@"C:\test.txt", 'A');
string text = "The quick brown fox jumps over the lazy dog";
int counter = 0;
for (int i = 0; i < text.Count(); i++)
{
    if (text[i] == 'a' || text[i] == 'A')
    {
        counter++;
    }
}
Console.WriteLine(counter);

请注意,如果文件确实存在,则此代码不会进行检查。如果输入的路径错误,则会出现
FileNotFoundException

如果不想使用foreach,则可以删除所有字母a并比较长度差。
有点像这样:

private static string analyseFile(string fileContents)
    {
        var strippedString = fileContents.Replace("A","");
        var count = fileContents.Length - strippedString.Length;

        return count.ToString();
    }
你可以用linq

string text = "The quick brown fox jumps over the lazy dog";
var count = text.ToLower().Where(x => x == 'a').Count();
Console.WriteLine(count);
但如果你不能使用任何先进的技术,你可以这样做:

public int LetterCount(string filename, char letter)
{
    int cnt = 0;
    string source = File.ReadAllText(filename);
    //Check every character in your string; if it matches increase the counter by 1
    foreach (char c in source)
    {
        if(c == letter)
        {
            cnt++;
        }
    }
    return cnt;
}
int A_count = LetterCount(@"C:\test.txt", 'A');
string text = "The quick brown fox jumps over the lazy dog";
int counter = 0;
for (int i = 0; i < text.Count(); i++)
{
    if (text[i] == 'a' || text[i] == 'A')
    {
        counter++;
    }
}
Console.WriteLine(counter);
string text=“敏捷的棕色狐狸跳过了懒狗”;
int计数器=0;
对于(int i=0;i
尽可能简单

    string test = "ABBCDABNDEAA";

    int Count = test.Count(x => x == 'A');

使用
LINQ
,这可以通过非常简单的方式实现:

string myString = "ababsgsdfsaaaAA22bbaa";

var count = myString.ToLower().Count(c => c == 'a');

Console.Write(count);

在这里,我们将字符串转换为所有小写形式,以便将
A
A
一起计算。然后我们使用简单的
LINQ
方法
Count()
来计算
a
字符的数量。

看看LINQ。它允许对任何类型的集合执行整个范围的操作。字符串是字符的集合。下面是LINQ如何让您的生活更轻松的示例:

string text = "A sdfsf a A sdfsf AAS sdfA";
int res = text.Count(letter => letter == 'A');
这里发生的事情是,您获取
文本
,并提供一个谓词,表示希望从字符串中获取任何变量
字母
,以便
字母
等于char
a
。然后你要数一数。你可以这样做:

        string stringValue = "Addsadsd AAf,,werAA";

        int qtdChar = stringValue.Count(x => x == 'A');
        int qtdCharInsensitive = stringValue.Count(x => x == 'A' || x=='a');

Foreach只是另一种类型的循环。这可以通过for循环轻松完成。诀窍是将字符串拆分为单个字符,以便以后比较

我相信,如果我让您走上正确的道路,您会明白如何实现这一点:

string test = "My name is Isak";
char[] arrayOfChars = test.ToCharArray();
int count = 0;

for (int i = 0; i < arrayOfChars.Length; i++)
{
     if (arrayOfChars[i] == 'a' || arrayOfChars[i] == 'A')
     {
          count++;
     }
}
string test=“我的名字是Isak”;
char[]arrayOfChars=test.ToCharArray();
整数计数=0;
for(int i=0;i
您需要以某种方式迭代文件的内容。您是否已经介绍了
循环的
?Serv的答案很容易适应
for
循环,因为您的印象是不应该使用
foreach
。然而,后者绝对是首选的做法。或者更简短一些:
File.ReadAllText(“Path”).Count(c=>c==“A”)
当我用Count@Ric阅读解决方案时,我不得不咯咯地笑。@Ric如果他担心
foreach
对于他在班上的位置来说太高级了,
Count
甚至更糟。@Rawling是的,我想是这样的,但看起来就是这样neat@mg30rg同意,这就是为什么我的解决方案是一个注释,而不是一个答案,因为它没有教授任何基本概念。为什么你提供烤箱准备好的代码?询问者显然希望学习,而不是为了一个完整的解决方案。它使意图变得不明显(类似于执行
i.ToString().Length==1
检查而不是
i>0&&i<10
:),虽然代码较少,但速度不必要地慢。他最初的问题是foreach太复杂,所以我认为linq也太复杂。就性能而言,我怀疑会有很大的不同。我甚至找到了一个声称replace方法优于foreach&linq的消息来源(尽管只有一小部分)。