For some reason, the 'instr' function and the 'string.indexof' is the same.
The problem is that one of the 2 string arguments, is a variable that is passed from the calling subroutine (not the literal characters) and it does not recognise the 'match'.
When I type in the literal characters with " " -- it works ok.
Here is a copy of the line that is not working.
'm' is the positional marker / 'largetxt' the body that is being searched and 'smalltxt' is the string that is being searched for.
intx = InStr(m, mainTxt, smallTxt)
The variable 'smallTxt' is the problem ---- if I have: intx = InStr(m, mainTxt, "house") --- it works ok
Also ... same thing for: intx = largeTxt.IndexOf(smallTxt, m)
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim maintext AsStringDim smalltext AsStringDim foundpos AsIntegerDim startpos AsInteger'startpos = 6 'should test not found
startpos = 0'should test matched
maintext = "Jack and Jill"
smalltext = "and"
foundpos = maintext.IndexOf(smalltext, startpos)
If foundpos > 0Then'found
MsgBox("match found at character " & foundpos)
MsgBox("no match found")
Thank you both .
It the 'indexof' function also not working .... it gives a true result may one time in 20 (when I know it is a true hit).
I think there may be a bug in vb.net. It doesn't matter what I do, it comes up with -1 all the time.
Did you try the code portion i added in my thread?
I tested it and it works fine...
obviously we can see the word AND is in the string JACK AND JILL.....
It makes me wonder what the ACTUAL strings are that you are comparing.
Maybe you could add them in a thread?
Its highly unlikely that the IndexOf method/function isnot working!
Its quite possible though that your installation of the development environment may have a corrupted file and possibly the next step would be to uninstall and re-install it again.
Still though, maybe you could save some time by adding your strings which your using in the comparision or even to upload the ACTUAL code block which is causing the offence?
Also, if your searching for a single character without specifying the number of consecutive characters to search, then the character must be in UNICODE, maybe that is of relavance?
Finally IndexOf is Zero based, so the FIRST character is position 0 then increases upwards, meaning it does not start with 1 but with zero.
Looking at the string "JACK AND JILL" and searching for a match of "AND" the code block I gave you states a match is found at charcter position 5, which taking it from a zero based index is ACTUALLY chracter 6 when starting from 1...
So maybe as a last resort you could subtract one from your start point (or start at zero anyway) and look for a match you KNOW exists in your string.