[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.
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
Re: RichTextBox Extended Find & Select
Quote:
Originally Posted by
Edgemeal
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.
Re: RichTextBox Extended Find & Select
Quote:
Originally Posted by
cevem
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
1 Attachment(s)
Re: RichTextBox Extended Find & Select
Quote:
Originally Posted by
Edgemeal
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.
Attachment 133155