[c#] Counting how many times a certain char appears in a string before any other char appears

I have many strings. Each string is prepended with at least 1 $. What is the best way to loop through the chars of each string to count how many $'s there are per string.

eg:

"$hello" - 1
"$$hello" - 2
"$$h$ello" - 2

This question is related to c#

The answer is


int count = myString.TakeWhile(c => c == '$').Count();

And without LINQ

int count = 0;
while(count < myString.Length && myString[count] == '$') count++;

//This code worked for me

class CountOfLettersOfString
{
    static void Main()
    {

        Console.WriteLine("Enter string to check count of letters");
        string name = Console.ReadLine();

        //Method1
        char[] testedalphabets = new char[26];
        int[] letterCount = new int[26];
        int countTestesd = 0;
        Console.WriteLine($"Given String is:{name}");

        for (int i = 0; i < name.Length - 1; i++)
        {
            int countChar = 1;

            bool isCharTested = false;

            for (int j = 0; j < testedalphabets.Length - 1; j++)
            {
                if (name[i] == testedalphabets[j])
                {
                    isCharTested = true;
                    break;
                }
            }

            if (!isCharTested)
            {
                testedalphabets[countTestesd] = name[i];

                for (int k = i + 1; k < name.Length - 1; k++)
                {
                    if (name[i] == name[k])
                    {
                        countChar++;
                    }
                }
                letterCount[countTestesd] = countChar;
                countTestesd++;

            }
            else
            {
                continue;
            }
        }

        for (int i = 0; i < testedalphabets.Length - 1; i++)
        {
            if (!char.IsLetter(testedalphabets[i]))
            {
                continue;
            }
            Console.WriteLine($"{testedalphabets[i]}-{letterCount[i]}");
        }


        //Method2
        var g = from c in name.ToLower().ToCharArray() // make sure that L and l are the same eg
                group c by c into m
                select new { Key = m.Key, Count = m.Count() };

        foreach (var item in g)
        {
            Console.WriteLine(string.Format("Character:{0} Appears {1} times", item.Key.ToString(), item.Count));
        }

        Console.ReadLine();
    }
}

This is a similar Solution to find how many email addresses included in a string. This way is more efficient`

int count = 0;
foreach (char c in email.Trim())
    if (c == '@') count++;

One approach you could take is the following method:

// Counts how many of a certain character occurs in the given string
public static int CharCountInString(char chr, string str)
{
    return str.Split(chr).Length-1;
}

As per the parameters this method returns the count of a specific character within a specific string.

This method works by splitting the string into an array by the specified character and then returning the length of that array -1.


You could do this, it doesn't require LINQ, but it's not the best way to do it(since you make split the whole string and put it in an array and just pick the length of it, you could better just do a while loop and check every character), but it works.

int count = test.Split('$').Length - 1;

just a simple answer:

    public static int CountChars(string myString, char myChar)
    {
        int count = 0;
        for (int i = 0; i < myString.Length; i++)
        {
            if (myString[i] == myChar) ++count;
        }
        return count;
    }

Cheers! - Rick


var str ="hello";
str.Where(c => c == 'l').Count()  // 2

You could use the Count method

var count = mystring.Count(x => x == '$')

int count = yourText.Length - yourText.TrimStart('$').Length;

int count = Regex.Matches(myString,"$").Count;

public static int GetHowManyTimeOccurenceCharInString(string text, char c)
{
    int count = 0;
    foreach(char ch in text)
    {
        if(ch.Equals(c))
        {
            count++;
        }

    }
    return count;
}

The simplest approach would be to use LINQ:

var count = text.TakeWhile(c => c == '$').Count();

There are certainly more efficient approaches, but that's probably the simplest.