Counting how many times a certain char appears in a string before any other char appears

前端 未结 12 819
再見小時候
再見小時候 2021-01-03 17:17

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 $\

相关标签:
12条回答
  • 2021-01-03 18:05
    var str ="hello";
    str.Where(c => c == 'l').Count()  // 2
    
    0 讨论(0)
  • 2021-01-03 18:06
    int count = Regex.Matches(myString,"$").Count;
    
    0 讨论(0)
  • 2021-01-03 18:07

    You could use the Count method

    var count = mystring.Count(x => x == '$')
    
    0 讨论(0)
  • 2021-01-03 18:07
    public static int GetHowManyTimeOccurenceCharInString(string text, char c)
    {
        int count = 0;
        foreach(char ch in text)
        {
            if(ch.Equals(c))
            {
                count++;
            }
    
        }
        return count;
    }
    
    0 讨论(0)
  • 2021-01-03 18:13

    //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();
        }
    }
    
    0 讨论(0)
  • 2021-01-03 18:16

    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++;
    
    0 讨论(0)
提交回复
热议问题