Count all occurrences of a character in a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thomaz

    Count all occurrences of a character in a string

    Hi....

    There is a string method to count the total number of a
    specified character in a string.
    EX: count the total of (*) in a string
    *** Test ***

    Thanks....

  • Peter Rilling

    #2
    Re: Count all occurrences of a character in a string

    You could use a regular expression.

    "thomaz" <suppa@nce.ufrj .br> wrote in message
    news:18ee01c4db a8$0372f270$a40 1280a@phx.gbl.. .[color=blue]
    > Hi....
    >
    > There is a string method to count the total number of a
    > specified character in a string.
    > EX: count the total of (*) in a string
    > *** Test ***
    >
    > Thanks....
    >[/color]


    Comment

    • Dennis Myrén

      #3
      Re: Count all occurrences of a character in a string

      I would recommend a manual approach, looping through each character in the
      string.

      This example could be optimized more.
      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;
      }


      --
      Regards,
      Dennis JD Myrén
      Oslo Kodebureau
      "thomaz" <suppa@nce.ufrj .br> wrote in message
      news:18ee01c4db a8$0372f270$a40 1280a@phx.gbl.. .[color=blue]
      > Hi....
      >
      > There is a string method to count the total number of a
      > specified character in a string.
      > EX: count the total of (*) in a string
      > *** Test ***
      >
      > Thanks....
      >[/color]


      Comment

      • Paul E Collins

        #4
        Re: Count all occurrences of a character in a string

        "thomaz" <suppa@nce.ufrj .br> wrote:
        [color=blue]
        > There is a string method to count the total number
        > of a specified character in a string.[/color]

        private static int CountChar(char c, string s)
        {
        int pos = 0, count = 0;

        while ((pos = s.IndexOf(c, pos)) != -1)
        {
        count++;
        pos++;
        }

        return count;
        }

        P.


        Comment

        • C# Learner

          #5
          Re: Count all occurrences of a character in a string

          thomaz <suppa@nce.ufrj .br> wrote:
          [color=blue]
          > There is a string method to count the total number of a
          > specified character in a string.
          > EX: count the total of (*) in a string
          > *** Test ***[/color]

          This is a nice way, IMO:

          public class StringHelper
          {
          public static int CountOccurences OfChar(string instance, char c) {
          int result = 0;
          foreach (char curChar in instance) {
          if (c == curChar) {
          ++result;
          }
          }
          return result;
          }
          }

          Comment

          Working...