I am building a list of locations for a word in a string with the While loop below.
When using IndexOf with StartingIndex can it repeat StartingIndex?
That is what I am finding. I have one place in the string it is at (19) and it finds it the first time, and then I have an endless loop.
Var ptr As Integer = 0
Var cnt As Integer = 0
While ptr > -1
ptr = TextBx.Value.IndexOf(ptr, LSrchWord)//
Plces.AddRow ptr
wend
TextBx is the TextArea
LSrchWord is the search string
Plces is an array of integers
How about:
var ptr as integer = TextBx.Value.IndexOf( LSrchWord )
while ptr <> -1
Places.AddRow ptr
ptr = TextBx.Value.IndexOf( ptr + LSrchWord.Length, LSrchWord )
wend
(Untested.)
Thanks. I’ll try that.
I assume including StartingIndex means the next value in the array has to be a higher value.
With InStr it does mean it has to be another value.
I know because I used InStr here before IndexOf.
Correct?
I also assume it means it will not recycle, but it does?
The first use does not include it, and your code never increments it, hence it “recycles”.
I did but without an incrementer.
ptr = TextBx.Value.IndexOf(ptr, LSrchWord)
Grumble. There is no example of it using Start in the LR.
Grumble. I think I’ve been spoiled by XOJO. I actually thought it took care of the increment.
I also like the way you start the while with
Plces.AddRow ptr
Think of it this way. Var Pos As Integer = abcd.IndexOf(a)
This code will not work, its just for demonstration. In this case Pos
will be 0 because what is returned is the index of the desired character. Think of it as 0 characters were skipped before the a was found.
When you try IndexOf(0, a)
you still get back zero because you told Xojo skip the first 0 characters, then start searching for a.
With InStr, the returned value was 1-based. This twists the logic a little. No longer does the phrase 1 character was skipped before the a was found work because its not true. The result means the a was found as the first character. It also means when you use the start parameter, the request is skip the first character, then start searching for a. In a sense, your goal happened because InStr returns a 1-based value, but the start position takes a 0-based value. So you kind of got that built-in incrementing effect.
Thanks for the explanation. I had to drop out of the college Logic class because of this sort of stuff.