列出字符串中的所有“A”

本文关键字:字符串 | 更新日期: 2023-09-27 18:36:48

我想计算一个paritcular 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)
        {
            //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;
        }
    }
}

列出字符串中的所有“A”

看看 LINQ。它允许对任何类型的集合执行全方位的操作。字符串是字符的集合。下面是一个示例,LINQ 如何使您的生活更轻松:

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

这里发生的事情是,你接受text并提供一个谓词,说你想从字符串中获取任何变量letter,使letter等于字符A。然后你想数它们。

最简单的方法之一是遍历文件中的所有字符,并检查字母是否等于您想要的字母。

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

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 只是另一种类型的循环。这可以通过 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 = "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

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 stringValue = "Addsadsd AAf,,werAA";
        int qtdChar = stringValue.Count(x => x == 'A');
        int qtdCharInsensitive = stringValue.Count(x => x == 'A' || x=='a');

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

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