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 24 2275
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" <de********@yahoo.com> schreef in bericht
news:%2****************@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
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 nj********@hotmail.com http://www.nathansokalski.com/
"Derek Hart" <de********@yahoo.com> wrote in message
news:%2****************@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
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
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
"Michel Posseth [MCP]" <mi****@posseth.com> schreef in bericht
news:Om**************@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" <de********@yahoo.com> schreef in bericht news:%2****************@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
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)
"Derek Hart" <de********@yahoo.com> wrote in message
news:%2****************@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
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" <de********@yahoo.com> schreef in bericht
news:%2****************@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
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#
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:u9**************@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" <de********@yahoo.com> schreef in bericht news:%2****************@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
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
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]" <no************@planet.nl> schreef in bericht
news:OI**************@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
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]" <no************@planet.nl> wrote in message
news:OI**************@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
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]" <no************@planet.nl> wrote in message
news:%2****************@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]" <no************@planet.nl> schreef in bericht news:OI**************@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
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" <Dr*************@hotmail.com> schreef in bericht
news:uV**************@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]" <no************@planet.nl> wrote in message news:OI**************@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
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]" <no************@planet.nl> schreef in bericht news:OI**************@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
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:eH**************@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]" <no************@planet.nl> schreef in bericht news:OI**************@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
You are correct Goran.
Cheers,
Greg
"Göran Andersson" <gu***@guffa.com> wrote in message
news:eH**************@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]" <no************@planet.nl> schreef in bericht news:OI**************@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
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: 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]" <no************@planet.nl> wrote in message news:%2****************@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]" <no************@planet.nl> schreef in bericht news:OI**************@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
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:eH**************@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]" <no************@planet.nl> schreef in bericht news:OI**************@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
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:O4**************@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:eH**************@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]" <no************@planet.nl> schreef in bericht news:OI**************@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 >
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?
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:O4**************@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:eH**************@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]" <no************@planet.nl> schreef in bericht > news:OI**************@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 >>
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. http://groups.google.com/group/micro...5c33cc87237dbf
By the way we are still waiting on that typed string from Herfried,
Cor
"Göran Andersson" <gu***@guffa.com> schreef in bericht
news:%2****************@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:O4**************@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:eH**************@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]" <no************@planet.nl> schreef in bericht >> news:OI**************@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 >>>
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. 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:eH**************@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]" <no************@planet.nl> schreef in bericht news:OI**************@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 >
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jon |
last post by:
I want to count the number of instances of a certain string(delimiter) 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...
|
by: Per Forsgren |
last post by:
I have a DLL with a public class (MyClass).
The class is instantiated into several instances in a Windows form with :
Protected WithEvents MyInst01 As New MyClass.MyClass
Protected WithEvents...
|
by: Kuups |
last post by:
Hi!
I have a question regarding the count if character within a string
like for example I have a string of
e.g.
123#123#
I would like to determine what is the code? of getting the # sign
|
by: Derek Hart |
last post by:
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...
|
by: jenkinsloveschicken |
last post by:
I am somewhat new to Access and have been tasked with creating a reporting database for my operation. The problem I am having is that I am needing to use the Count function to calculate total...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |