Home All Groups Group Topic Archive Search About

Count Instances Of String Within String

Author
7 May 2006 5:04 PM
Derek Hart
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

Author
7 May 2006 5:53 PM
Michel Posseth [MCP]
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]


Show quoteHide quote
"Derek Hart" <derekmh***@yahoo.com> schreef in bericht
news:%23x99NifcGHA.3952@TK2MSFTNGP04.phx.gbl...
> 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
>
Author
7 May 2006 6:16 PM
Michel Posseth [MCP]
Ahum :-(

Embarased mode :

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Dim s As String = "I want to go to the park, and then go home."
        MsgBox(countsubstrings(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




Show quoteHide quote
"Michel Posseth [MCP]" <mic***@posseth.com> schreef in bericht
news:OmwGe9fcGHA.4108@TK2MSFTNGP03.phx.gbl...
> 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" <derekmh***@yahoo.com> schreef in bericht
> news:%23x99NifcGHA.3952@TK2MSFTNGP04.phx.gbl...
>> 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
>>
>
>
Author
7 May 2006 5:54 PM
Nathan Sokalski
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.IndexOf("go", i + 1) <> -1
stringcount += 1
i = teststring.IndexOf("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
njsokal***@hotmail.com
http://www.nathansokalski.com/

Show quoteHide quote
"Derek Hart" <derekmh***@yahoo.com> wrote in message
news:%23x99NifcGHA.3952@TK2MSFTNGP04.phx.gbl...
> 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
>
Author
7 May 2006 6:05 PM
jayeldee
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.RegularExpressions

Module main
    Sub main()

        Dim strtoSearch As String = "I want to go to the park, and then
go home."
        Console.WriteLine(GetStringOccurences(strtoSearch,
"go").ToString)
        Console.ReadLine()

    End Sub

    Private Function GetStringOccurences(ByVal searchString, ByVal
searchWord) As Integer

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

        Return r.Matches(searchString).Count

    End Function

End Module
Author
8 May 2006 12:01 AM
+Vice
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(wordcount)

Show quoteHide quote
"Derek Hart" <derekmh***@yahoo.com> wrote in message
news:%23x99NifcGHA.3952@TK2MSFTNGP04.phx.gbl...
> 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
>
Author
8 May 2006 6:35 AM
Cor Ligthert [MVP]
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(ByVal 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

Show quoteHide quote
"Derek Hart" <derekmh***@yahoo.com> schreef in bericht
news:%23x99NifcGHA.3952@TK2MSFTNGP04.phx.gbl...
> 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
>
Author
8 May 2006 8:45 AM
Greg Young
Cor ...

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

Cheers,

Greg Young
MVP - C#
Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:u9riclmcGHA.3908@TK2MSFTNGP04.phx.gbl...
> 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(ByVal 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" <derekmh***@yahoo.com> schreef in bericht
> news:%23x99NifcGHA.3952@TK2MSFTNGP04.phx.gbl...
>> 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
>>
>
>
Author
8 May 2006 9:02 AM
Cor Ligthert [MVP]
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(tofind, "");
> 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
Author
8 May 2006 9:21 AM
Cor Ligthert [MVP]
It does probably, I misreaded something, I am testing it, what it real means
because I am in doubt about some side effects.

Cor

Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schreef in bericht
news:OIiJw3ncGHA.1208@TK2MSFTNGP02.phx.gbl...
> 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(tofind, "");
>> 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
>
Author
8 May 2006 9:30 AM
Greg Young
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#
Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:%23mJl6BocGHA.1208@TK2MSFTNGP02.phx.gbl...
> 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]" <notmyfirstn***@planet.nl> schreef in bericht
> news:OIiJw3ncGHA.1208@TK2MSFTNGP02.phx.gbl...
>> 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(tofind, "");
>>> 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
>>
>
>
Author
8 May 2006 10:55 AM
Göran_Andersson
That is neatly handled by a regular expression. The /b code matches a
word boundary.

RegEx re = new RegEx("/b" + RegEx.Escape(tofind) + "/b");
int found = re.Matches(foo).Count;

Greg Young wrote:
Show quoteHide quote
> 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]" <notmyfirstn***@planet.nl> wrote in message
> news:%23mJl6BocGHA.1208@TK2MSFTNGP02.phx.gbl...
>> 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]" <notmyfirstn***@planet.nl> schreef in bericht
>> news:OIiJw3ncGHA.1208@TK2MSFTNGP02.phx.gbl...
>>> 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(tofind, "");
>>>> 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
>>>
>>
>
>
Author
8 May 2006 2:10 PM
jayeldee
Thanks for this, Goran.   I had posted something similar earlier, but I
had used the "/s" which did not correctly catch instances of the string
at the start or end of a sentence.

Is the RegEx.Escape( ) there to account for any characters in your
toFind variable that are RegEx operators?
Author
8 May 2006 2:36 PM
Göran_Andersson
jayeldee wrote:
> Is the RegEx.Escape( ) there to account for any characters in your
> toFind variable that are RegEx operators?

Exactly. Although a typical word wouldn't contain any, it keeps the
regular expression from crashing if it would.
Author
8 May 2006 10:00 AM
Göran_Andersson
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:
Show quoteHide quote
> 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]" <notmyfirstn***@planet.nl> schreef in bericht
> news:OIiJw3ncGHA.1208@TK2MSFTNGP02.phx.gbl...
>> 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(tofind, "");
>>> 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
>>
>
>
Author
8 May 2006 10:10 AM
Cor Ligthert [MVP]
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

Show quoteHide quote
"Göran Andersson" <gu***@guffa.com> schreef in bericht
news:eHs4RZocGHA.3952@TK2MSFTNGP04.phx.gbl...
> 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:
>> 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]" <notmyfirstn***@planet.nl> schreef in bericht
>> news:OIiJw3ncGHA.1208@TK2MSFTNGP02.phx.gbl...
>>> 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(tofind, "");
>>>> 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
>>>
>>
Author
8 May 2006 11:37 AM
Göran_Andersson
If you have come to that conclusion I think that you haven't used them
in the same way, as both internally calls
CurrentCulture.CompareInfo.IndexOf. InStr only has more overhead.

Cor Ligthert [MVP] wrote:
Show quoteHide quote
> 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" <gu***@guffa.com> schreef in bericht
> news:eHs4RZocGHA.3952@TK2MSFTNGP04.phx.gbl...
>> 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:
>>> 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]" <notmyfirstn***@planet.nl> schreef in bericht
>>> news:OIiJw3ncGHA.1208@TK2MSFTNGP02.phx.gbl...
>>>> 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(tofind, "");
>>>>> 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
>>>>
>
Author
8 May 2006 1:32 PM
Cor Ligthert [MVP]
Goran,

Did you test it, I had before I tested it the same idea as you. Instr is a
very simple instruction, while indexof has more complex posibilities. Those
should be tested of course.

And as I said not with char.

Cor

Show quoteHide quote
"Göran Andersson" <gu***@guffa.com> schreef in bericht
news:O4nWCPpcGHA.2068@TK2MSFTNGP02.phx.gbl...
> If you have come to that conclusion I think that you haven't used them in
> the same way, as both internally calls CurrentCulture.CompareInfo.IndexOf.
> InStr only has more overhead.
>
> Cor Ligthert [MVP] wrote:
>> 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" <gu***@guffa.com> schreef in bericht
>> news:eHs4RZocGHA.3952@TK2MSFTNGP04.phx.gbl...
>>> 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:
>>>> 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]" <notmyfirstn***@planet.nl> schreef in bericht
>>>> news:OIiJw3ncGHA.1208@TK2MSFTNGP02.phx.gbl...
>>>>> 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(tofind, "");
>>>>>> 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
>>>>>
>>
Author
8 May 2006 2:29 PM
Göran_Andersson
It would be interresting to se how you did the test. When I test it I
get the result that IndexOf is slightly faster, but they never differ
more than a few percent.

Here is what I tested:


HighResolutionClock clock;
int pos;
double time1, time2;
string text, find;

text = "askdjf iuqwh peha sduuhböaos9 döqown eiluhas9ödhföoasid öfoiajsd
fä0sd föoiqwe fuh asilduhfasudh föoiqiweöf oihas dlifyg asliudhf
öoasihdf h";
find = "duhf";

clock = new HighResolutionClock();

clock.Reset();
for (int i = 0; i < 1000000; i++) pos = Strings.InStr(2, text, find,
CompareMethod.Text);
time1 = clock.Seconds;

clock.Reset();
for (int i = 0; i < 1000000; i++) pos = text.IndexOf(find, 1,
StringComparison.CurrentCultureIgnoreCase);
time2 = clock.Seconds;


Typical result:

time1: 3.589
time2: 3.519


(I can post the HighResolutionClock class if you want. It uses the
QueryPerformanceCounter method in kernel32.dll.)


Cor Ligthert [MVP] wrote:
Show quoteHide quote
> Goran,
>
> Did you test it, I had before I tested it the same idea as you. Instr is a
> very simple instruction, while indexof has more complex posibilities. Those
> should be tested of course.
>
> And as I said not with char.
>
> Cor
>
> "Göran Andersson" <gu***@guffa.com> schreef in bericht
> news:O4nWCPpcGHA.2068@TK2MSFTNGP02.phx.gbl...
>> If you have come to that conclusion I think that you haven't used them in
>> the same way, as both internally calls CurrentCulture.CompareInfo.IndexOf.
>> InStr only has more overhead.
>>
>> Cor Ligthert [MVP] wrote:
>>> 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" <gu***@guffa.com> schreef in bericht
>>> news:eHs4RZocGHA.3952@TK2MSFTNGP04.phx.gbl...
>>>> 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:
>>>>> 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]" <notmyfirstn***@planet.nl> schreef in bericht
>>>>> news:OIiJw3ncGHA.1208@TK2MSFTNGP02.phx.gbl...
>>>>>> 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(tofind, "");
>>>>>>> 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
>>>>>>
>
>
Author
8 May 2006 3:07 PM
Cor Ligthert [MVP]
http://groups.google.com/group/microsoft.public.dotnet.languages.vb/msg/315c33cc87237dbf

By the way we are still waiting on that typed string from Herfried,

Cor


Show quoteHide quote
"Göran Andersson" <gu***@guffa.com> schreef in bericht
news:%23KnwevqcGHA.4312@TK2MSFTNGP05.phx.gbl...
> It would be interresting to se how you did the test. When I test it I get
> the result that IndexOf is slightly faster, but they never differ more
> than a few percent.
>
> Here is what I tested:
>
>
> HighResolutionClock clock;
> int pos;
> double time1, time2;
> string text, find;
>
> text = "askdjf iuqwh peha sduuhböaos9 döqown eiluhas9ödhföoasid öfoiajsd
> fä0sd föoiqwe fuh asilduhfasudh föoiqiweöf oihas dlifyg asliudhf öoasihdf
> h";
> find = "duhf";
>
> clock = new HighResolutionClock();
>
> clock.Reset();
> for (int i = 0; i < 1000000; i++) pos = Strings.InStr(2, text, find,
> CompareMethod.Text);
> time1 = clock.Seconds;
>
> clock.Reset();
> for (int i = 0; i < 1000000; i++) pos = text.IndexOf(find, 1,
> StringComparison.CurrentCultureIgnoreCase);
> time2 = clock.Seconds;
>
>
> Typical result:
>
> time1: 3.589
> time2: 3.519
>
>
> (I can post the HighResolutionClock class if you want. It uses the
> QueryPerformanceCounter method in kernel32.dll.)
>
>
> Cor Ligthert [MVP] wrote:
>> Goran,
>>
>> Did you test it, I had before I tested it the same idea as you. Instr is
>> a very simple instruction, while indexof has more complex posibilities.
>> Those should be tested of course.
>>
>> And as I said not with char.
>>
>> Cor
>>
>> "Göran Andersson" <gu***@guffa.com> schreef in bericht
>> news:O4nWCPpcGHA.2068@TK2MSFTNGP02.phx.gbl...
>>> If you have come to that conclusion I think that you haven't used them
>>> in the same way, as both internally calls
>>> CurrentCulture.CompareInfo.IndexOf. InStr only has more overhead.
>>>
>>> Cor Ligthert [MVP] wrote:
>>>> 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" <gu***@guffa.com> schreef in bericht
>>>> news:eHs4RZocGHA.3952@TK2MSFTNGP04.phx.gbl...
>>>>> 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:
>>>>>> 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]" <notmyfirstn***@planet.nl> schreef in bericht
>>>>>> news:OIiJw3ncGHA.1208@TK2MSFTNGP02.phx.gbl...
>>>>>>> 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(tofind, "");
>>>>>>>> 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
>>>>>>>
>>
Author
10 May 2006 12:10 PM
david
On 2006-05-08, Göran Andersson <gu***@guffa.com> wrote:
> If you have come to that conclusion I think that you haven't used them
> in the same way, as both internally calls
> CurrentCulture.CompareInfo.IndexOf. InStr only has more overhead.

A slight correction, by default InStr uses InvariantCulture, while
String.IndexOf always uses CurrentCulture.



Show quoteHide quote
>
> Cor Ligthert [MVP] wrote:
>> 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" <gu***@guffa.com> schreef in bericht
>> news:eHs4RZocGHA.3952@TK2MSFTNGP04.phx.gbl...
>>> 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:
>>>> 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]" <notmyfirstn***@planet.nl> schreef in bericht
>>>> news:OIiJw3ncGHA.1208@TK2MSFTNGP02.phx.gbl...
>>>>> 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(tofind, "");
>>>>>> 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
>>>>>
>>
Author
10 May 2006 6:52 PM
Göran_Andersson
david wrote:
> On 2006-05-08, Göran Andersson <gu***@guffa.com> wrote:
>> If you have come to that conclusion I think that you haven't used them
>> in the same way, as both internally calls
>> CurrentCulture.CompareInfo.IndexOf. InStr only has more overhead.
>
> A slight correction, by default InStr uses InvariantCulture, while
> String.IndexOf always uses CurrentCulture.
>

Yes, if you do a binary search it will use InvariantCulture. So will
IndexOf.
Author
8 May 2006 10:13 AM
Greg Young
You are correct Goran.

Cheers,

Greg
Show quoteHide quote
"Göran Andersson" <gu***@guffa.com> wrote in message
news:eHs4RZocGHA.3952@TK2MSFTNGP04.phx.gbl...
> 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:
>> 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]" <notmyfirstn***@planet.nl> schreef in bericht
>> news:OIiJw3ncGHA.1208@TK2MSFTNGP02.phx.gbl...
>>> 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(tofind, "");
>>>> 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
>>>
>>
Author
8 May 2006 9:23 AM
Greg Young
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(ByVal 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(ByVal SearchItem As String, ByVal
ToCountString As String) As Integer
        Dim tmp As String = SearchItem.Replace(ToCountString, "")
        Return (SearchItem.Length - tmp.Length) / ToCountString.Length
    End Function

    Sub Main()
        Dim ToCountString As String = "test"
        Dim SearchItem As String = "testtesttestfootesttesttest"
        Dim i As Integer
        Dim starttime As DateTime = DateTime.Now
        Dim endtime As DateTime
        For i = 0 To 1000000
            CountString(SearchItem, ToCountString)
        Next
        endtime = DateTime.Now
        Console.WriteLine("CountString - " & (endtime -
starttime).ToString())
        starttime = DateTime.Now
        For i = 0 To 1000000
            CountString2(SearchItem, ToCountString)
        Next
        endtime = DateTime.Now
        Console.WriteLine("CountString2 - " & (endtime -
starttime).ToString())

    End Sub

End Module
Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:OIiJw3ncGHA.1208@TK2MSFTNGP02.phx.gbl...
> 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(tofind, "");
>> 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
>
Author
8 May 2006 9:48 AM
Cor Ligthert [MVP]
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

Show quoteHide quote
"Greg Young" <DruckDruckGo***@hotmail.com> schreef in bericht
news:uVRYdEocGHA.3352@TK2MSFTNGP03.phx.gbl...
>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(ByVal 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(ByVal SearchItem As String, ByVal
> ToCountString As String) As Integer
>        Dim tmp As String = SearchItem.Replace(ToCountString, "")
>        Return (SearchItem.Length - tmp.Length) / ToCountString.Length
>    End Function
>
>    Sub Main()
>        Dim ToCountString As String = "test"
>        Dim SearchItem As String = "testtesttestfootesttesttest"
>        Dim i As Integer
>        Dim starttime As DateTime = DateTime.Now
>        Dim endtime As DateTime
>        For i = 0 To 1000000
>            CountString(SearchItem, ToCountString)
>        Next
>        endtime = DateTime.Now
>        Console.WriteLine("CountString - " & (endtime -
> starttime).ToString())
>        starttime = DateTime.Now
>        For i = 0 To 1000000
>            CountString2(SearchItem, ToCountString)
>        Next
>        endtime = DateTime.Now
>        Console.WriteLine("CountString2 - " & (endtime -
> starttime).ToString())
>
>    End Sub
>
> End Module
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
> news:OIiJw3ncGHA.1208@TK2MSFTNGP02.phx.gbl...
>> 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(tofind, "");
>>> 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
>>
>
>