Results 1 to 4 of 4

Thread: C# count occurrences in a string

  1. #1
    Join Date
    Feb 2009
    Posts
    71

    C# count occurrences in a string

    I need to have the simple way to count the number of occurences of all elements of a list into that same list in C# I know this is possible and also used in many applications but I did not able to solve this problem.

  2. #2
    Join Date
    Feb 2006
    Posts
    159

    Re: C# count occurrences in a string

    In the Main method, which we use to test the CountStringOccurrences method, we see that "Sam" occurs twice in the example string, matching the requirement. There is a performance cost to the Split method. If the only operation on the string is to count the words, you should consider using the Matches or IndexOf methods instead.It is usually best to simplify the branches in your code, avoiding as many conditionals/ifs as possible. This loop only tests one condition, the result of IndexOf.

  3. #3
    Join Date
    Dec 2008
    Posts
    202

    Re: C# count occurrences in a string

    Try following example, and check if it works as per your requirement if yes then you may use your numbers to get the expected result i found this code on the internet.

    Code:
    var l1 = new List<int>() { 1,2,3,4,5,2,2,2,4,4,4,1 };
    
    var g = l1.GroupBy( i => i );
    
    foreach( var grp in g )
    {
      Console.WriteLine( "{0} {1}", grp.Key, grp.Count() );
    }

  4. #4
    Join Date
    Feb 2009
    Posts
    105

    Re: C# count occurrences in a string

    In the above mentioned program outer loop of looping over all the words in the list is unnecessary and will cause problems to count the occurrence of a string. Remove the outer loop and it looks like what you have should work properly. However, if performance is not a critical issue, or you have already split the sentence in order to perform other types of queries over it, then it makes sense to use LINQ to count the words or phrases as well. following is the C++ code but this is very clear and easy to understand.

    Code:
    public static int CountChar ( string input, char c )
    {
    int retval = 0;
    for (int i = 0; i < input.Length; i ++)
    if (c == input [i])
    retval ++;
    return retval;
    }

Similar Threads

  1. Replies: 6
    Last Post: 25-02-2011, 10:40 AM
  2. Redirect Count down
    By puk284 in forum Software Development
    Replies: 1
    Last Post: 28-10-2010, 03:30 AM
  3. Formula to count the occurrences of specific letter in excel
    By Mithun Seth in forum Windows Software
    Replies: 5
    Last Post: 09-01-2010, 01:03 PM
  4. How to Manipulate String using PHP String Functions
    By ComPaCt in forum Software Development
    Replies: 3
    Last Post: 21-09-2009, 09:07 AM
  5. What is Regexp count
    By GunFighter in forum Software Development
    Replies: 3
    Last Post: 06-08-2009, 02:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,885,063.79682 seconds with 17 queries