Instances of a Char in String - C#


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Instances of a Char in String - C#

  1. #1
    Join Date
    May 2006
    Posts
    40

    Instances of a Char in String - C#

    I need to find out how many times a specified character occurs in a string.
    Here is how I plan on doing it (Or something like this as I just typed this now):

    Code:
    int i = 0, chr = 0;
    while (chr != -1)
    {
      if (string.indexof(specifiedChar, chr) != -1)
      {
        chr = string.indexof(specifiedChar, chr);
        i ++;
      }
      else
      {
       chr = -1;
      }
    }
    What I wondered was if there is a better way or even an inbuilt function that I've over looked?
    Last edited by xnemsis; 08-26-2006 at 01:36 PM.
    J JJ

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    That's one way to do it. I would probably only use indexOf to find multi-character substrings within a larger string. To count occurrences of an individual character, I'd probably do this:
    Code:
    public static int CharOccurs(string stringToSearch, char charToFind)
    {
        int count = 0;
        char[] chars = stringToSearch.ToCharArray();
        foreach (char c in chars)
        {
            if (c == charToFind)
            {
                count++;
            }
        }
        return count;
    }
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Dec 2005
    Location
    South of Nowhere, Maine
    Posts
    309
    Another method is to take the length of the string.
    Then take and remove all instances of "X" in the string. (where X is the chars you want).
    Now take the length of the resulting string.

    Use some simple math to determine the number of instances of "X".

  4. #4
    Join Date
    Mar 2005
    Posts
    71
    You may use regular expression (The class Regex under namespace System.Text.RegularExpressions)

    string exp = "c"; //prepare the expression. suppose you want to find the number of 'c'
    int numOfOccurence = Regex.Matches(str, exp).Count;

  5. #5
    Join Date
    Aug 2006
    Posts
    38
    You may also calculate the length after replacing the char with empty space. For eg if you want to find the number of 'x' characters in a string s:

    s.Length - s.Replace("x", "").Length will return you the number of 'x' characters in the string.

  6. #6
    Join Date
    Dec 2005
    Location
    South of Nowhere, Maine
    Posts
    309
    rupakguha, see my response two posts up.

  7. #7
    Join Date
    Aug 2010
    Posts
    2
    Ok.. It is probably way too late..
    but for those who still read this..

    int count = StringContainingText.Split( specificChar ).Length -1;

  8. #8
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Never too late for an answer Martijn. Thanks for posting and Welcome to DevX
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  9. #9
    Join Date
    Nov 2010
    Posts
    1

    Thumbs up Perfect!

    Quote Originally Posted by Martijn View Post
    Ok.. It is probably way too late..
    but for those who still read this..

    int count = StringContainingText.Split( specificChar ).Length -1;
    @Martijn: THANK YOU! Your contribution was EXACTLY what I needed. I was looking for something that would read all the text in a file and count how many times a character (user specified) was found. I was stuck at the last part, thanks for your post!

Similar Threads

  1. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 03:32 PM
  2. Replies: 1
    Last Post: 06-05-2001, 07:12 AM
  3. VB/C Array parameters
    By Gastao Woelfert in forum VB Classic
    Replies: 2
    Last Post: 09-01-2000, 12:36 PM
  4. VB/C Array parameters
    By Gastao Woelfert in forum VB Classic
    Replies: 0
    Last Post: 09-01-2000, 09:59 AM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 04:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center