Count Instances Of String Within String

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

    Count Instances Of String Within String

    Is there an efficient line of code to count the number of instances of one
    string within another.

    If I have the sentence:
    "I want to go to the park, and then go home."

    It would give me a count of 2 for the word "go"

    Derek


  • Michel Posseth [MCP]

    #2
    Re: Count Instances Of String Within String

    use the string compare method

    Dim s As String = "I want to go to the park, and then go home."
    MsgBox((String. Compare(s, "go") + 1).ToString & " Occurances of go")

    regards

    Michel Posseth [MCP]


    "Derek Hart" <derekmhart@yah oo.com> schreef in bericht
    news:%23x99Nifc GHA.3952@TK2MSF TNGP04.phx.gbl. ..[color=blue]
    > Is there an efficient line of code to count the number of instances of one
    > string within another.
    >
    > If I have the sentence:
    > "I want to go to the park, and then go home."
    >
    > It would give me a count of 2 for the word "go"
    >
    > Derek
    >[/color]


    Comment

    • Nathan Sokalski

      #3
      Re: Count Instances Of String Within String

      I don't believe there is a way to put it in one line (at least not in
      VB.NET, other languages might have a built-in method that does this or
      possibly give you the ability to put multiple commands on one line), but
      here is a simple loop that does what you want in VB.NET:


      Dim teststring As String = "I want to go to the park, and then go home."
      Dim i As Integer = -1
      Dim stringcount As Integer = 0
      While teststring.Inde xOf("go", i + 1) <> -1
      stringcount += 1
      i = teststring.Inde xOf("go", i + 1)
      End While


      You can also write it as a function, which I would strongly suggest if you
      plan on doing this more than once:


      Public Function CountInstances( ByVal lookfor As String, ByVal lookin As
      String) As Integer
      Dim stringcount As Integer = 0
      Dim i As Integer = -1
      While lookin.IndexOf( lookfor, i + 1) <> -1
      stringcount += 1
      i = lookin.IndexOf( lookfor, i + 1)
      End While
      Return stringcount
      End Function


      Writing it as a function will require the few lines of code to write the
      function, but after that you can call it from just one line, making your
      code simpler to write and debug:


      stringcount = CountInstances( "go", teststring)


      If you have any questions, feel free to ask. Good Luck!
      --
      Nathan Sokalski
      njsokalski@hotm ail.com
      有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


      "Derek Hart" <derekmhart@yah oo.com> wrote in message
      news:%23x99Nifc GHA.3952@TK2MSF TNGP04.phx.gbl. ..[color=blue]
      > Is there an efficient line of code to count the number of instances of one
      > string within another.
      >
      > If I have the sentence:
      > "I want to go to the park, and then go home."
      >
      > It would give me a count of 2 for the word "go"
      >
      > Derek
      >[/color]


      Comment

      • jayeldee

        #4
        Re: Count Instances Of String Within String

        Derek,

        You could also use regular expressions to match the instances of a
        string inside another string. I *believe* the pattern I use returns
        all instances of 'go' surrounded by spaces, but then again I'm not very
        good with RegEx yet.


        Imports System.Text.Reg ularExpressions

        Module main
        Sub main()

        Dim strtoSearch As String = "I want to go to the park, and then
        go home."
        Console.WriteLi ne(GetStringOcc urences(strtoSe arch,
        "go").ToStr ing)
        Console.ReadLin e()

        End Sub

        Private Function GetStringOccure nces(ByVal searchString, ByVal
        searchWord) As Integer

        Dim r As New Regex(String.Fo rmat("\s{0}\s", searchWord))

        Return r.Matches(searc hString).Count

        End Function

        End Module

        Comment

        • Michel Posseth [MCP]

          #5
          Re: Count Instances Of String Within String


          Ahum :-(

          Embarased mode :

          Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
          System.EventArg s) Handles Button1.Click
          Dim s As String = "I want to go to the park, and then go home."
          MsgBox(countsub strings(s, "go"))

          End Sub
          Function countsubstrings (ByVal source As String, ByVal search As String)
          As Integer
          Dim count As Integer = -1
          Dim index As Integer = -1
          Do
          count += 1
          index = source.IndexOf( search, index + 1)
          Loop Until index < 0
          Return count
          End Function

          this wil work




          "Michel Posseth [MCP]" <michel@posseth .com> schreef in bericht
          news:OmwGe9fcGH A.4108@TK2MSFTN GP03.phx.gbl...[color=blue]
          > use the string compare method
          >
          > Dim s As String = "I want to go to the park, and then go home."
          > MsgBox((String. Compare(s, "go") + 1).ToString & " Occurances of
          > go")
          >
          > regards
          >
          > Michel Posseth [MCP]
          >
          >
          > "Derek Hart" <derekmhart@yah oo.com> schreef in bericht
          > news:%23x99Nifc GHA.3952@TK2MSF TNGP04.phx.gbl. ..[color=green]
          >> Is there an efficient line of code to count the number of instances of
          >> one string within another.
          >>
          >> If I have the sentence:
          >> "I want to go to the park, and then go home."
          >>
          >> It would give me a count of 2 for the word "go"
          >>
          >> Derek
          >>[/color]
          >
          >[/color]


          Comment

          • +Vice

            #6
            Re: Count Instances Of String Within String

            Dim str$ = "I want to go to the park, and then go home."
            Dim findstr$ = "go"
            Dim wordcount% = (Len(str) - Len(Replace(str , findstr, ""))) / Len(findstr)
            MsgBox(wordcoun t)

            "Derek Hart" <derekmhart@yah oo.com> wrote in message
            news:%23x99Nifc GHA.3952@TK2MSF TNGP04.phx.gbl. ..[color=blue]
            > Is there an efficient line of code to count the number of instances of one
            > string within another.
            >
            > If I have the sentence:
            > "I want to go to the park, and then go home."
            >
            > It would give me a count of 2 for the word "go"
            >
            > Derek
            >[/color]


            Comment

            • Cor Ligthert [MVP]

              #7
              Re: Count Instances Of String Within String

              Derek,

              As once tested in this newsgroup is this the fastest method for that (I have
              changed the fieldnames now so watch that).
              \\\
              Public Function CountString(ByV al SearchItem As String, ByVal ToCountString
              _
              As String) As Integer
              Dim Start as Integer = 1
              Dim Count as Interger = 0
              Dim Result as Interger
              Do
              Result = InStr(Start, SearchItem, ToCountString)
              If Result = 0 Then Exit Do
              Count += 1
              Start = Result + 1
              Loop
              Return Count
              End Function
              ///

              If the ToCountString becomes a ToCountChar than there are better methods.

              I hope this helps,

              Cor

              "Derek Hart" <derekmhart@yah oo.com> schreef in bericht
              news:%23x99Nifc GHA.3952@TK2MSF TNGP04.phx.gbl. ..[color=blue]
              > Is there an efficient line of code to count the number of instances of one
              > string within another.
              >
              > If I have the sentence:
              > "I want to go to the park, and then go home."
              >
              > It would give me a count of 2 for the word "go"
              >
              > Derek
              >[/color]


              Comment

              • Greg Young

                #8
                Re: Count Instances Of String Within String

                Cor ...

                surely the following will be much faster.
                string tofind = "test"
                string foo = "testtest footestfoo";
                string changed = foo.Replace(tof ind, "");
                return (foo.length - changed.length) / tofind.length

                Cheers,

                Greg Young
                MVP - C#
                "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
                news:u9riclmcGH A.3908@TK2MSFTN GP04.phx.gbl...[color=blue]
                > Derek,
                >
                > As once tested in this newsgroup is this the fastest method for that (I
                > have changed the fieldnames now so watch that).
                > \\\
                > Public Function CountString(ByV al SearchItem As String, ByVal
                > ToCountString _
                > As String) As Integer
                > Dim Start as Integer = 1
                > Dim Count as Interger = 0
                > Dim Result as Interger
                > Do
                > Result = InStr(Start, SearchItem, ToCountString)
                > If Result = 0 Then Exit Do
                > Count += 1
                > Start = Result + 1
                > Loop
                > Return Count
                > End Function
                > ///
                >
                > If the ToCountString becomes a ToCountChar than there are better methods.
                >
                > I hope this helps,
                >
                > Cor
                >
                > "Derek Hart" <derekmhart@yah oo.com> schreef in bericht
                > news:%23x99Nifc GHA.3952@TK2MSF TNGP04.phx.gbl. ..[color=green]
                >> Is there an efficient line of code to count the number of instances of
                >> one string within another.
                >>
                >> If I have the sentence:
                >> "I want to go to the park, and then go home."
                >>
                >> It would give me a count of 2 for the word "go"
                >>
                >> Derek
                >>[/color]
                >
                >[/color]


                Comment

                • Cor Ligthert [MVP]

                  #9
                  Re: Count Instances Of String Within String

                  Greg,

                  I am sure it will not.
                  [color=blue]
                  >
                  > surely the following will be much faster.
                  > string tofind = "test"
                  > string foo = "testtest footestfoo";
                  > string changed = foo.Replace(tof ind, "");
                  > return (foo.length - changed.length) / tofind.length
                  >[/color]
                  Although some little changes can make that it probably does.

                  :-)

                  I said I thought yesterday already that I found it a nice idea.

                  I forgot that with some changes you can use it for this as well.

                  Cor


                  Comment

                  • Cor Ligthert [MVP]

                    #10
                    Re: Count Instances Of String Within String

                    It does probably, I misreaded something, I am testing it, what it real means
                    because I am in doubt about some side effects.

                    Cor

                    "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> schreef in bericht
                    news:OIiJw3ncGH A.1208@TK2MSFTN GP02.phx.gbl...[color=blue]
                    > Greg,
                    >
                    > I am sure it will not.
                    >[color=green]
                    >>
                    >> surely the following will be much faster.
                    >> string tofind = "test"
                    >> string foo = "testtest footestfoo";
                    >> string changed = foo.Replace(tof ind, "");
                    >> return (foo.length - changed.length) / tofind.length
                    >>[/color]
                    > Although some little changes can make that it probably does.
                    >
                    > :-)
                    >
                    > I said I thought yesterday already that I found it a nice idea.
                    >
                    > I forgot that with some changes you can use it for this as well.
                    >
                    > Cor
                    >[/color]


                    Comment

                    • Greg Young

                      #11
                      Re: Count Instances Of String Within String

                      I am a strong believer in the pragmatic programmer idea of ... if in doubt
                      test it ...

                      added a loop to run 100 tests, countstring2 beat countstring 100/100 times
                      ... even with varying data counts (including no data)... perhaps you have
                      some code shoing the opposite?

                      Cheers,

                      Greg Young
                      MVP - C#

                      Module Module1

                      Public Function CountString(ByV al SearchItem As String, ByVal
                      ToCountString As String) As Integer
                      Dim Start As Integer = 1
                      Dim Count As Integer = 0
                      Dim Result As Integer
                      Do
                      Result = InStr(Start, SearchItem, ToCountString)
                      If Result = 0 Then Exit Do
                      Count += 1
                      Start = Result + 1
                      Loop
                      Return Count
                      End Function

                      Public Function CountString2(By Val SearchItem As String, ByVal
                      ToCountString As String) As Integer
                      Dim tmp As String = SearchItem.Repl ace(ToCountStri ng, "")
                      Return (SearchItem.Len gth - tmp.Length) / ToCountString.L ength
                      End Function

                      Sub Main()
                      Dim ToCountString As String = "test"
                      Dim SearchItem As String = "testtesttestfo otesttesttest"
                      Dim i As Integer
                      Dim starttime As DateTime = DateTime.Now
                      Dim endtime As DateTime
                      For i = 0 To 1000000
                      CountString(Sea rchItem, ToCountString)
                      Next
                      endtime = DateTime.Now
                      Console.WriteLi ne("CountStrin g - " & (endtime -
                      starttime).ToSt ring())
                      starttime = DateTime.Now
                      For i = 0 To 1000000
                      CountString2(Se archItem, ToCountString)
                      Next
                      endtime = DateTime.Now
                      Console.WriteLi ne("CountString 2 - " & (endtime -
                      starttime).ToSt ring())

                      End Sub

                      End Module
                      "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
                      news:OIiJw3ncGH A.1208@TK2MSFTN GP02.phx.gbl...[color=blue]
                      > Greg,
                      >
                      > I am sure it will not.
                      >[color=green]
                      >>
                      >> surely the following will be much faster.
                      >> string tofind = "test"
                      >> string foo = "testtest footestfoo";
                      >> string changed = foo.Replace(tof ind, "");
                      >> return (foo.length - changed.length) / tofind.length
                      >>[/color]
                      > Although some little changes can make that it probably does.
                      >
                      > :-)
                      >
                      > I said I thought yesterday already that I found it a nice idea.
                      >
                      > I forgot that with some changes you can use it for this as well.
                      >
                      > Cor
                      >[/color]


                      Comment

                      • Greg Young

                        #12
                        Re: Count Instances Of String Within String

                        The problem exists in both methods in use ..

                        If I want to look for the word "GO" and I also have worgs like gong or pogo,
                        they will be detected as being instances of the word, an easy way to work
                        around this is to pass in spaces i.e. " go " but then I will not detect
                        patterns such as "lets go!" because there is no traling space or "go to the
                        beach" because there is no leading space.

                        It is these items that make the implementation of an algorithm like this
                        tricky.

                        Cheers,

                        Greg Young
                        MVP - C#
                        "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
                        news:%23mJl6Boc GHA.1208@TK2MSF TNGP02.phx.gbl. ..[color=blue]
                        > It does probably, I misreaded something, I am testing it, what it real
                        > means because I am in doubt about some side effects.
                        >
                        > Cor
                        >
                        > "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> schreef in bericht
                        > news:OIiJw3ncGH A.1208@TK2MSFTN GP02.phx.gbl...[color=green]
                        >> Greg,
                        >>
                        >> I am sure it will not.
                        >>[color=darkred]
                        >>>
                        >>> surely the following will be much faster.
                        >>> string tofind = "test"
                        >>> string foo = "testtest footestfoo";
                        >>> string changed = foo.Replace(tof ind, "");
                        >>> return (foo.length - changed.length) / tofind.length
                        >>>[/color]
                        >> Although some little changes can make that it probably does.
                        >>
                        >> :-)
                        >>
                        >> I said I thought yesterday already that I found it a nice idea.
                        >>
                        >> I forgot that with some changes you can use it for this as well.
                        >>
                        >> Cor
                        >>[/color]
                        >
                        >[/color]


                        Comment

                        • Cor Ligthert [MVP]

                          #13
                          Re: Count Instances Of String Within String

                          Greg,

                          The problem as you wrote was my idea too, however that side effect was not
                          there in my simple test. 100* a string "Cor Greg GregCor CorGreg ", that I
                          tested 100.000 times each time both methods and counting the time.

                          The method with the replace beats the methode with the moving instr at least
                          about 5:7.

                          Both methods are therefore quicker than any I have seen until now, while
                          that replace method is for me now the fastest.

                          Cor

                          "Greg Young" <DruckDruckGoos e@hotmail.com> schreef in bericht
                          news:uVRYdEocGH A.3352@TK2MSFTN GP03.phx.gbl...[color=blue]
                          >I am a strong believer in the pragmatic programmer idea of ... if in doubt
                          >test it ...
                          >
                          > added a loop to run 100 tests, countstring2 beat countstring 100/100 times
                          > .. even with varying data counts (including no data)... perhaps you have
                          > some code shoing the opposite?
                          >
                          > Cheers,
                          >
                          > Greg Young
                          > MVP - C#
                          >
                          > Module Module1
                          >
                          > Public Function CountString(ByV al SearchItem As String, ByVal
                          > ToCountString As String) As Integer
                          > Dim Start As Integer = 1
                          > Dim Count As Integer = 0
                          > Dim Result As Integer
                          > Do
                          > Result = InStr(Start, SearchItem, ToCountString)
                          > If Result = 0 Then Exit Do
                          > Count += 1
                          > Start = Result + 1
                          > Loop
                          > Return Count
                          > End Function
                          >
                          > Public Function CountString2(By Val SearchItem As String, ByVal
                          > ToCountString As String) As Integer
                          > Dim tmp As String = SearchItem.Repl ace(ToCountStri ng, "")
                          > Return (SearchItem.Len gth - tmp.Length) / ToCountString.L ength
                          > End Function
                          >
                          > Sub Main()
                          > Dim ToCountString As String = "test"
                          > Dim SearchItem As String = "testtesttestfo otesttesttest"
                          > Dim i As Integer
                          > Dim starttime As DateTime = DateTime.Now
                          > Dim endtime As DateTime
                          > For i = 0 To 1000000
                          > CountString(Sea rchItem, ToCountString)
                          > Next
                          > endtime = DateTime.Now
                          > Console.WriteLi ne("CountStrin g - " & (endtime -
                          > starttime).ToSt ring())
                          > starttime = DateTime.Now
                          > For i = 0 To 1000000
                          > CountString2(Se archItem, ToCountString)
                          > Next
                          > endtime = DateTime.Now
                          > Console.WriteLi ne("CountString 2 - " & (endtime -
                          > starttime).ToSt ring())
                          >
                          > End Sub
                          >
                          > End Module
                          > "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
                          > news:OIiJw3ncGH A.1208@TK2MSFTN GP02.phx.gbl...[color=green]
                          >> Greg,
                          >>
                          >> I am sure it will not.
                          >>[color=darkred]
                          >>>
                          >>> surely the following will be much faster.
                          >>> string tofind = "test"
                          >>> string foo = "testtest footestfoo";
                          >>> string changed = foo.Replace(tof ind, "");
                          >>> return (foo.length - changed.length) / tofind.length
                          >>>[/color]
                          >> Although some little changes can make that it probably does.
                          >>
                          >> :-)
                          >>
                          >> I said I thought yesterday already that I found it a nice idea.
                          >>
                          >> I forgot that with some changes you can use it for this as well.
                          >>
                          >> Cor
                          >>[/color]
                          >
                          >[/color]


                          Comment

                          • Göran Andersson

                            #14
                            Re: Count Instances Of String Within String

                            The replace method might be more efficient for short strings, but the
                            method using InStr (or IndexOf) scales better, as it doesn't create
                            another string that is almost as big as the string being searched.

                            Cor Ligthert [MVP] wrote:[color=blue]
                            > It does probably, I misreaded something, I am testing it, what it real means
                            > because I am in doubt about some side effects.
                            >
                            > Cor
                            >
                            > "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> schreef in bericht
                            > news:OIiJw3ncGH A.1208@TK2MSFTN GP02.phx.gbl...[color=green]
                            >> Greg,
                            >>
                            >> I am sure it will not.
                            >>[color=darkred]
                            >>> surely the following will be much faster.
                            >>> string tofind = "test"
                            >>> string foo = "testtest footestfoo";
                            >>> string changed = foo.Replace(tof ind, "");
                            >>> return (foo.length - changed.length) / tofind.length
                            >>>[/color]
                            >> Although some little changes can make that it probably does.
                            >>
                            >> :-)
                            >>
                            >> I said I thought yesterday already that I found it a nice idea.
                            >>
                            >> I forgot that with some changes you can use it for this as well.
                            >>
                            >> Cor
                            >>[/color]
                            >
                            >[/color]

                            Comment

                            • Cor Ligthert [MVP]

                              #15
                              Re: Count Instances Of String Within String

                              Goran,

                              Indexof with strings is twice as slow as InStr.

                              With char it beats InStr that it is not to mention, but it is than of course
                              comparing apples with pears, because InStr(char) does not exist.

                              Cor

                              "Göran Andersson" <guffa@guffa.co m> schreef in bericht
                              news:eHs4RZocGH A.3952@TK2MSFTN GP04.phx.gbl...[color=blue]
                              > The replace method might be more efficient for short strings, but the
                              > method using InStr (or IndexOf) scales better, as it doesn't create
                              > another string that is almost as big as the string being searched.
                              >
                              > Cor Ligthert [MVP] wrote:[color=green]
                              >> It does probably, I misreaded something, I am testing it, what it real
                              >> means because I am in doubt about some side effects.
                              >>
                              >> Cor
                              >>
                              >> "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> schreef in bericht
                              >> news:OIiJw3ncGH A.1208@TK2MSFTN GP02.phx.gbl...[color=darkred]
                              >>> Greg,
                              >>>
                              >>> I am sure it will not.
                              >>>
                              >>>> surely the following will be much faster.
                              >>>> string tofind = "test"
                              >>>> string foo = "testtest footestfoo";
                              >>>> string changed = foo.Replace(tof ind, "");
                              >>>> return (foo.length - changed.length) / tofind.length
                              >>>>
                              >>> Although some little changes can make that it probably does.
                              >>>
                              >>> :-)
                              >>>
                              >>> I said I thought yesterday already that I found it a nice idea.
                              >>>
                              >>> I forgot that with some changes you can use it for this as well.
                              >>>
                              >>> Cor
                              >>>[/color]
                              >>[/color][/color]

                              Comment

                              Working...