Results 1 to 5 of 5

Thread: Find String Within String = Cant be found!?!?

  1. #1

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    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))

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Find String Within String = Cant be found!?!?

    Thanks Ill look at that.

  4. #4
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    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.
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

  5. #5

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    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
    Last edited by joefox; Apr 28th, 2011 at 10:08 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width