String instance count

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

    #1

    String instance count

    I want to count the number of instances of a certain string(delimite r) in
    another string. I didn't see a function to do this in the framework (if
    there is, please point me to it). If not, could someone let me know if the
    method I've used below is efficient or if there is a better way to do it, as
    these will be rather large strings I'm searching in. Thanks

    Public Shared Function CountDelimiter( ByVal strInput As String, ByVal
    strDelimiter As String) As Int32
    Dim iStart As Int32, iCount As Int32, iResult As Int32

    'Set our vars to base values
    iStart = 1
    iCount = 0

    Do
    'iResult becomes the position where delimiter is found. If 0, not
    found.
    iResult = InStr(iStart, strInput, strDelimiter)
    If iResult = 0 Then Exit Do
    'Increment our count var for each time it is found
    iCount += 1
    'Increment our next start position to be the next char after the
    currently found position
    iStart = iResult + 1
    Loop

    Return iCount
    End Function



    --
    --------
    Jon Rosenberg


  • Cor

    #2
    Re: String instance count

    Hi Jon,
    I am too always looking for that, but when I did look at your code I thought
    this stupid
    \\\\\\\
    Dim a As String = "abaaa abbba babaabaabaab"
    Dim b As String() = Split(a, "ab")
    MessageBox.Show ((b.Length - 1).ToString)
    ///////
    When you don't get a better answer you can try it, doing some little tests
    it did work.
    It is stupid of course.
    Cor


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: String instance count

      "Cor" <non@non.com> scripsit:[color=blue]
      > I am too always looking for that, but when I did look at your code I thought
      > this stupid
      > \\\\\\\
      > Dim a As String = "abaaa abbba babaabaabaab"
      > Dim b As String() = Split(a, "ab")
      > MessageBox.Show ((b.Length - 1).ToString)
      > ///////
      > When you don't get a better answer you can try it, doing some little tests
      > it did work.
      > It is stupid of course.[/color]

      Yes, it works. But I think it's not the best solution if you split a 10
      MB string.

      ;-)

      --
      Herfried K. Wagner
      MVP · VB Classic, VB.NET
      <http://www.mvps.org/dotnet>

      Comment

      • Cor

        #4
        Re: String instance count

        Herfried,
        The other samples does as far as I can see a loop from 1 increment per
        character each time.
        I am not so sure if that is faster,
        But ok lets make a deal, do you type a 10Mb string, than will I make the
        testprogram.
        Cor


        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: String instance count

          Cor,
          When I need a 10Mb string, I normally use a loop with a StringBuilder. As I
          suspect posting a 10M string to the newsgroup might be blocked ;-)

          Something like:

          Dim sb As New System.Text.Str ingBuilder(10 * 1024 * 1024)

          For i As Integer = 0 To 1024 * 1024
          sb.Append("0123 456789")
          Next

          Dim s As String = sb.ToString()

          Sometimes I will use a System.Random object to indirectly create random text
          to append instead of the fixed values.

          A week or two ago the topic of counting occurrences of delimiters in strings
          came up in the C# newsgroup, someone suggested using a
          System.Text.Reg ularExpression. RegEx object. One of the individuals did some
          testing, the RegEx method was about 100 times slower than the for loop. They
          did not try the Split method. This was based on occurrences of individual
          characters, not occurrences of strings. See "string manipulation" from 25
          Sept 2003 in the microsoft.publi c.dotnet.langua ges.csharp newsgroup if you
          are interested.

          Out of curiosity it would be interesting to see what the difference between
          splitting, counting & reg expressions performance wise... On both VB.NET
          2002 & VB.NET 2003...

          Hope this helps
          Jay

          "Cor" <non@non.com> wrote in message
          news:3f7c99b5$0 $18955$48b97d01 @reader22.wxs.n l...[color=blue]
          > Herfried,
          > The other samples does as far as I can see a loop from 1 increment per
          > character each time.
          > I am not so sure if that is faster,
          > But ok lets make a deal, do you type a 10Mb string, than will I make the
          > testprogram.
          > Cor
          >
          >[/color]


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: String instance count

            "Cor" <non@non.com> scripsit:[color=blue]
            > The other samples does as far as I can see a loop from 1 increment per
            > character each time.
            > I am not so sure if that is faster,
            > But ok lets make a deal, do you type a 10Mb string, than will I make the
            > testprogram.[/color]

            Splitting a 10 MB sting will increase memory usage for the application
            to 20 MB. That's what I wanted to say.

            :-)

            --
            Herfried K. Wagner
            MVP · VB Classic, VB.NET
            <http://www.mvps.org/dotnet>

            Comment

            • Cor

              #7
              Re: String instance count

              Hi Jay,
              Me too,
              I shall try to make a test and give the results here.
              But I don't have vb2002 and I am always to lazy to make a regex.
              But I have a link, someone did send for a regex maker, maybe I can use that.
              Otherwise it is only the two methods, (maybe three so that I can use
              Microsoft Visual basic too)
              I thought that Jeremy Cowles said in the chat that they where so terrible
              slow.

              I make that string myself of course, but look at the post I send to
              Herfried.

              Normaly you see the results tomorrow.
              Cor


              Comment

              • Cor

                #8
                Re: String instance count

                Hi Herfried,
                I forgot to tell, when you are ready with typing that teststring from 10Mb.
                please don't zip it in the post, just paste it in.
                With those problems from the newserver from Microsoft, I think a zip file
                never arives.
                Cor


                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: String instance count

                  "Cor" <non@non.com> scripsit:[color=blue]
                  > I forgot to tell, when you are ready with typing that teststring from 10Mb.
                  > please don't zip it in the post, just paste it in.
                  > With those problems from the newserver from Microsoft, I think a zip file
                  > never arives.[/color]

                  I think there will be a size limitation...

                  --
                  Herfried K. Wagner
                  MVP · VB Classic, VB.NET
                  <http://www.mvps.org/dotnet>

                  Comment

                  • Cor

                    #10
                    Re: String instance count

                    Herfried,
                    I have seen you are very quick in typing. That will give no size problem.
                    Cor


                    Comment

                    • Chris Dunaway

                      #11
                      Re: String instance count

                      "Jon" <ruffles_@msn.c om> wrote in
                      news:vnovsedmlp cd57@corp.super news.com:
                      [color=blue]
                      > I want to count the number of instances of a certain string(delimite r)[/color]

                      One method would be to use a regular expression and then check the
                      Matches.Count property:


                      '\\\\
                      Dim sSource As String = "abbabaabbababa babbababbabbaab ababbbabbababab babab"
                      Dim rx As New System.Text.Reg ularExpressions .Regex("a")

                      MsgBox("Number of matches = " & CStr(rx.Matches (sSource).Count ))
                      '////

                      Comment

                      • therealpmuk
                        New Member
                        • Jul 2006
                        • 1

                        #12
                        I also have an interest in this, needing to check for (and count) the number of occurrences of various errors in an error.log file.

                        The files can get quite large, but I have achieved the following results with a filesize of just over 11 Meg:-


                        Count 79608

                        TimeBySplit 22187500 Long
                        TimeByLoop 28750000 Long
                        TimeByRegEx 1093750 Long


                        Count 113

                        TimeBySplit 2031250 Long
                        TimeByLoop 28125000 Long
                        TimeByRegEx 156250 Long

                        Timing done using System.DateTime .Now.Ticks.

                        As you can see, the Regular Expression method consistently yields the fastest results.

                        Comment

                        Working...