Results 1 to 5 of 5

Thread: [RESOLVED] RichTextBox Extended Find & Select

  1. #1

    Thread Starter
    Lively Member cevem's Avatar
    Join Date
    Feb 2010
    Location
    Mars
    Posts
    78

    Resolved [RESOLVED] RichTextBox Extended Find & Select

    Hello. Sorry for my bad English.

    Am trying to select specific text from a loaded file. But i need more then... My Richboxtext name = menustream

    Let me explain:

    Here is my text line from loaded file
    Code:
    SkinVersion{	text="Funky Application - 1.0.2" shellcmd="" }
    I need this text
    Code:
    Funky Application - 1.0.2
    Everything is fine, but version number in text e.g. "1.0.2" is variable... How i get the "Funky Application - %vernumber%" in this RichTextBox?

    I hope asked the question understandable...

    Thank you.
    Last edited by cevem; Dec 12th, 2015 at 10:43 AM. Reason: corrections

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: RichTextBox Extended Find & Select

    If you know what the pre-text and post-text are you can use basic string methods like IndexOf, LastIndexOf to find where that text is located in the string and then use SubString to get just the needed text, there is also RegEx which is best for complicated senerios and is another whole language in itself to learn, and one I picked up many years ago in classic vb using Split, that I still use occasionally, its slow but easy to use,

    for example to get Funky Application - 1.0.2 from, SkinVersion{ text="Funky Application - 1.0.2" shellcmd="" }
    you could pass this function the pretext of text=" and post text which is the char " .

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim neededText As String = GetTextBetween(lineOfText, "text=""", """")
            MessageBox.Show(neededText)
        End Sub
    
        Private Function GetTextBetween(MainText As String, StartText As String, EndText As String, Optional occurance As Integer = 1) As String
            If occurance < 1 Then occurance = 1
            Try
                Return MainText.Split({StartText}, StringSplitOptions.None)(occurance).Split({EndText}, StringSplitOptions.None)(0)
            Catch
                Return ""
            End Try
        End Function
    Last edited by Edgemeal; Dec 12th, 2015 at 12:09 PM.

  3. #3

    Thread Starter
    Lively Member cevem's Avatar
    Join Date
    Feb 2010
    Location
    Mars
    Posts
    78

    Re: RichTextBox Extended Find & Select

    Quote Originally Posted by Edgemeal View Post
    If you know what the pre-text and post-text are you can use basic string methods like IndexOf, LastIndexOf to find where that text is located in the string and then use SubString to get just the needed text, there is also RegEx which is best for complicated senerios and is another whole language in itself to learn, and one I picked up many years ago in classic vb using Split, that I still use occasionally, its slow but easy to use,

    for example to get Funky Application - 1.0.2 from, SkinVersion{ text="Funky Application - 1.0.2" shellcmd="" }
    you could pass this function the pretext of text=" and post text which is the char " .

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim neededText As String = GetTextBetween(lineOfText, "text=""", """")
            MessageBox.Show(neededText)
        End Sub
    
        Private Function GetTextBetween(MainText As String, StartText As String, EndText As String, Optional occurance As Integer = 1) As String
            If occurance < 1 Then occurance = 1
            Try
                Return MainText.Split({StartText}, StringSplitOptions.None)(occurance).Split({EndText}, StringSplitOptions.None)(0)
            Catch
                Return ""
            End Try
        End Function
    Hello Edgemeal, thank you for the reply. Am totally new this... Please explain more of then.

    Here is the text file: http://pastebin.com/jmCxzmYF

    Thanks.

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: RichTextBox Extended Find & Select

    Quote Originally Posted by cevem View Post
    Hello Edgemeal, thank you for the reply. Am totally new this... Please explain more of then.
    Nothing to explain, if you trying to get text between known text then that is just one possible way to do that, same type of function but uses IndexOf & Substring...
    Code:
        Private Function TextBetween(mainText As String, findFirst As String, findSecond As String) As String
            Try
                Dim leftBracket = mainText.IndexOf(findFirst) + findFirst.Length
                Dim riteBracket = mainText.IndexOf(findSecond, leftBracket)
                Return mainText.Substring(leftBracket, riteBracket - leftBracket)
            Catch ex As Exception            
                Return "" ' Error?
            End Try
        End Function

  5. #5

    Thread Starter
    Lively Member cevem's Avatar
    Join Date
    Feb 2010
    Location
    Mars
    Posts
    78

    Re: RichTextBox Extended Find & Select

    Quote Originally Posted by Edgemeal View Post
    Nothing to explain, if you trying to get text between known text then that is just one possible way to do that, same type of function but uses IndexOf & Substring...
    Code:
        Private Function TextBetween(mainText As String, findFirst As String, findSecond As String) As String
            Try
                Dim leftBracket = mainText.IndexOf(findFirst) + findFirst.Length
                Dim riteBracket = mainText.IndexOf(findSecond, leftBracket)
                Return mainText.Substring(leftBracket, riteBracket - leftBracket)
            Catch ex As Exception            
                Return "" ' Error?
            End Try
        End Function
    Thank you so much, i got it and made it. Have a great day.

    Name:  screenshot_82.png
Views: 202
Size:  46.4 KB

Tags for this Thread

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