Find String Within String = Cant be found!?!?
I was trying to do a simiple, fine a string start position, or a string, and it always returns 0
Dim My_Search_String As String
Dim My_Text As String
My_Search_String = "fsl fwb fcb"
My_Text = "here is some kind of tex fsl fwb fcb"
MessageBox.Show(InStr(1, My_Search_String, My_Text))
Re: Find String Within String = Cant be found!?!?
You're using VB.NET, so use VB.NET. Call IndexOf on your original String and pass your substring as an argument. That will either return the index of the first occurrence of the substring or else -1 if the substring isn't found.
Also, make sure you have the Strings the right way round when you use IndexOf. You have them the wrong way round at the moment, which would explain why you don't get a result? I'm not sure why I even ask but did you read the documentation to make sure that you were calling the method correctly?
Finally, I think that anyone with over 1200 posts has been coding long enough to turn Option Strict On. You should turn it On in the IDE options and then it will be On by default for every project you create.
Re: Find String Within String = Cant be found!?!?
Re: Find String Within String = Cant be found!?!?
I was just looking at that. In Visual Studio it's under Debug > Options and Settings > Projects and Solutions > VB Defaults.
Re: Find String Within String = Cant be found!?!?
I was able to get this to work below
Dim My_Text As String
My_Text = "here is some kind of tex fsl fwb fcb some other text cool stop"
Dim bd_fieldNum As Integer = 1
Dim bd_beginStr As String = String.Format("fsl fwb fcb", bd_fieldNum)
Dim bd_beginPos As Integer = My_Text.IndexOf(bd_beginStr) + bd_beginStr.Length
Dim bd_result As String = My_Text.Substring(bd_beginPos, My_Text.IndexOf("stop", bd_beginPos) - bd_beginPos)
MessageBox.Show(bd_result)
However, when i changed it to this below, and used a dynamic set of text to search, it couldnt find anything. I did a check and popped up a message to show me the innerhtml of the element, and it was in there.
Dim My_Text As String
My_Text = element.InnerHtml.ToString
Dim bd_fieldNum As Integer = 1
Dim bd_beginStr As String = String.Format("fsl fwb fcb", bd_fieldNum)
Dim bd_beginPos As Integer = My_Text.IndexOf(bd_beginStr) + bd_beginStr.Length
Dim bd_result As String = My_Text.Substring(bd_beginPos, My_Text.IndexOf("</a>", bd_beginPos) - bd_beginPos)
MessageBox.Show(bd_result)
*********
i wanted to search for "tom" in the string "hello tom is here", i want it to give me back a number where "tom" first appears, then strip off anything before it, so the string now would be "is here" but i dont see that...
In my example i try to pull parts out, so i wanted an ending flag, that i could have, so it could get even "tom was" if "here" was my ending flag