string manipulation

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

    #1

    string manipulation

    Is there a way to search a string for the number of times
    a certain character shows up without having to write my
    own function?
    string var = "111-222-33.33-2"
    var.countchar(v ar) = 3
  • Jon Skeet

    #2
    Re: string manipulation

    Kevin <sdfkj@spamthis .com> wrote:[color=blue]
    > Is there a way to search a string for the number of times
    > a certain character shows up without having to write my
    > own function?
    > string var = "111-222-33.33-2"
    > var.countchar(v ar) = 3[/color]

    Your example doesn't make a lot of sense - what is it counting there?
    You haven't specified a character.

    There's no function that I know of which does that - but then it's not
    something that needs to be done very often. It's extremely easy to do
    though:

    int Count (string haystack, char needle)
    {
    int count=0;
    foreach (char c in haystack)
    {
    if (c==needle)
    count++;
    }
    return count;
    }

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Ralph Gerbig

      #3
      Re: string manipulation

      Hi,
      parse the string with a regex put the matches in a MatchCollection and take
      the count Property of the MatchCollection

      --
      Mit freundlichen Gruessen - Regards

      Ralph Gerbig
      ikea.ralph@web. de



      Comment

      • Jon Skeet

        #4
        Re: string manipulation

        Ralph Gerbig <ikea.ralph@web .de> wrote:[color=blue]
        > parse the string with a regex put the matches in a MatchCollection and take
        > the count Property of the MatchCollection[/color]

        That sounds like an awful lot of work though for something which is
        pretty simple. I personally think that regular expressions are rather
        overused. They're incredibly powerful, but sometimes a nail is just a
        nail, and doesn't need a powertool, just a hammer :)

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Fritz Switzer

          #5
          Re: string manipulation

          Ralph,

          Can you use Jon's needle and haystack example and let's see what approach is
          preferable?


          int Count (string haystack, char needle)
          {
          int count=0;
          foreach (char c in haystack)
          {
          if (c==needle)
          count++;
          }
          return count;
          }



          Fritz



          "Ralph Gerbig" <ikea.ralph@web .de> wrote in message
          news:ee29YK6gDH A.3616@TK2MSFTN GP11.phx.gbl...[color=blue]
          > Hi,
          > parse the string with a regex put the matches in a MatchCollection and[/color]
          take[color=blue]
          > the count Property of the MatchCollection
          >
          > --
          > Mit freundlichen Gruessen - Regards
          >
          > Ralph Gerbig
          > ikea.ralph@web. de
          > www.ralphgerbig.de.vu
          >
          >[/color]


          Comment

          • Bret Mulvey

            #6
            Re: string manipulation

            It's an interesting .NET performance question. Just to see how fast each
            technique was I wrote a quick test to compare three methods:

            Method 1 uses a foreach loop to retrieve each character in the string.

            Method 2 uses ToCharArray() to get all the characters in the string, then
            uses a for loop (not foreach) to go through each character in the array.

            Method 3 uses a Regex and gets the Regex.Matches.C ount property.

            With a test string that contains 83 characters (arbitrary) it turns out that
            method 2 is about five or six times faster than method 1, and about a
            hundred times faster than method 3.

            With a test string that contains 1000 characters method 2 gets even better,
            outperforming method 1 by 7.5 to 1 and method 3 by 150 to 1.

            Modifying method 2 to use foreach instead of for didn't measureably change
            the performance.

            Here's method 2 code:

            int CountChar(strin g s, char ch)
            {
            int count = 0;
            char[] a = s.ToCharArray() ;
            foreach (char c in a)
            if (c == ch)
            count++;
            return count;
            }


            "Jon Skeet" <skeet@pobox.co m> wrote in message
            news:MPG.19dd7e 17f2816c2b98976 c@news.microsof t.com...[color=blue]
            > Ralph Gerbig <ikea.ralph@web .de> wrote:[color=green]
            > > parse the string with a regex put the matches in a MatchCollection and[/color][/color]
            take[color=blue][color=green]
            > > the count Property of the MatchCollection[/color]
            >
            > That sounds like an awful lot of work though for something which is
            > pretty simple. I personally think that regular expressions are rather
            > overused. They're incredibly powerful, but sometimes a nail is just a
            > nail, and doesn't need a powertool, just a hammer :)
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet
            > If replying to the group, please do not mail me too[/color]


            Comment

            • Jon Skeet

              #7
              Re: string manipulation

              Bret Mulvey <bretm@microsof t.nospam0000.co m> wrote:[color=blue]
              > It's an interesting .NET performance question. Just to see how fast each
              > technique was I wrote a quick test to compare three methods:
              >
              > Method 1 uses a foreach loop to retrieve each character in the string.
              >
              > Method 2 uses ToCharArray() to get all the characters in the string, then
              > uses a for loop (not foreach) to go through each character in the array.
              >
              > Method 3 uses a Regex and gets the Regex.Matches.C ount property.
              >
              > With a test string that contains 83 characters (arbitrary) it turns out that
              > method 2 is about five or six times faster than method 1, and about a
              > hundred times faster than method 3.[/color]

              Out of interest, which version of .NET are you using? I believe that
              foreach iteration over a string is *much* better with 1.1 than with
              1.0.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Ralph Gerbig

                #8
                Re: string manipulation

                Hi,
                It would be three lines :)

                --
                Mit freundlichen Gruessen - Regards

                Ralph Gerbig
                ikea.ralph@web. de



                Comment

                • Jon Skeet

                  #9
                  Re: string manipulation

                  Ralph Gerbig <ikea.ralph@web .de> wrote:[color=blue]
                  > It would be three lines :)[/color]

                  But those three lines would in turn do an awful lot more work - I
                  suspect you'd find it's very inefficient, and those lines themselves
                  are *relatively* complicated (that's relative to just counting the
                  occurrences of the character directly).

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • Bret Mulvey

                    #10
                    Re: string manipulation

                    It's 1.0. I'll try it with 1.1 to compare.

                    "Jon Skeet" <skeet@pobox.co m> wrote in message
                    news:MPG.19de44 ede0c27f7598976 e@msnews.micros oft.com...[color=blue]
                    > Bret Mulvey <bretm@microsof t.nospam0000.co m> wrote:[color=green]
                    > > It's an interesting .NET performance question. Just to see how fast each
                    > > technique was I wrote a quick test to compare three methods:
                    > >
                    > > Method 1 uses a foreach loop to retrieve each character in the string.
                    > >
                    > > Method 2 uses ToCharArray() to get all the characters in the string,[/color][/color]
                    then[color=blue][color=green]
                    > > uses a for loop (not foreach) to go through each character in the array.
                    > >
                    > > Method 3 uses a Regex and gets the Regex.Matches.C ount property.
                    > >
                    > > With a test string that contains 83 characters (arbitrary) it turns out[/color][/color]
                    that[color=blue][color=green]
                    > > method 2 is about five or six times faster than method 1, and about a
                    > > hundred times faster than method 3.[/color]
                    >
                    > Out of interest, which version of .NET are you using? I believe that
                    > foreach iteration over a string is *much* better with 1.1 than with
                    > 1.0.
                    >
                    > --
                    > Jon Skeet - <skeet@pobox.co m>
                    > http://www.pobox.com/~skeet
                    > If replying to the group, please do not mail me too[/color]


                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: string manipulation

                      Jon,
                      I would have used String.IndexOf to find each occurrence of the character,
                      then I saw your foreach example and thought "Doh! that's simpler".

                      Something like:

                      int Count(string haystack, char needle)
                      {
                      int count = 0;
                      for (int index = haystack.IndexO f(needle); index > 0; index =
                      haystack.IndexO f(needle, ++index))
                      count++;
                      return count;
                      }

                      Which I would not expect to be more efficient then the foreach loop.

                      Just another thought on the subject,

                      Jay

                      "Jon Skeet" <skeet@pobox.co m> wrote in message
                      news:MPG.19df68 e89e0313398977e @msnews.microso ft.com...[color=blue]
                      > Ralph Gerbig <ikea.ralph@web .de> wrote:[color=green]
                      > > It would be three lines :)[/color]
                      >
                      > But those three lines would in turn do an awful lot more work - I
                      > suspect you'd find it's very inefficient, and those lines themselves
                      > are *relatively* complicated (that's relative to just counting the
                      > occurrences of the character directly).
                      >
                      > --
                      > Jon Skeet - <skeet@pobox.co m>
                      > http://www.pobox.com/~skeet
                      > If replying to the group, please do not mail me too[/color]


                      Comment

                      Working...