列出字符串中的所有 "A"

List all of the "A"'s in string

我想计算特定字符串中的所有 "A's"。

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');

请注意,此代码不会检查文件是否确实存在。如果你输入错误的路径,你最终会得到 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);

但是如果您不能使用任何高级技术,您可以这样做:

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 test = "ABBCDABNDEAA";

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

使用LINQ,这真的很简单:

string myString = "ababsgsdfsaaaAA22bbaa";

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

Console.Write(count);

这里我们取字符串并将其转换为全部小写,这样 Aa 将一起计算。然后我们使用简单的LINQ方法Count()来统计有a个字符。

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

string text = "A sdfsf a A sdfsf AAS sdfA";
int res = text.Count(letter => letter == 'A');

这里发生的是,您使用 text 并提供一个谓词,说明您想要从字符串中获取任何变量 letter,使得 letter 等于 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++;
     }
}