I am trying to get the position of a certain string with in a textbox
e.g.
textbox1.text = "hello everyone, nice weather today!"
if string1 = nice,
position as integer will be 17
How do I get this integer through code?
Printable View
I am trying to get the position of a certain string with in a textbox
e.g.
textbox1.text = "hello everyone, nice weather today!"
if string1 = nice,
position as integer will be 17
How do I get this integer through code?
look for indexof
You can use instr to return the position:
Dim position as integer = instr(textbox1.text, string1)
If you want it to be case insensitive then use:
instr(textbox1.text, string1, CompareMethod.Text)
instr - old school.
instr may be old school but it does return the zero based position (17 in the example) - IndexOf returns 16.
Not that it really matters so long as you know which you are getting.
but did the OP know that (almost?) everything is zero-based, and therefore 16 was really the correct answer? instr returns a 1 Based value.
Code:Dim aString As String = "hello everyone, nice weather today!"
Debug.WriteLine(InStr(aString, "nice").ToString)
Debug.WriteLine(aString.IndexOf("nice").ToString)
That's what I meant to say - got it backwards when I posted (not paying enough attention, sorry).Quote:
instr returns a 1 Based value
Thanks to all for your help!