-
Dec 12th, 2015, 10:34 AM
#1
Thread Starter
Lively Member
[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
-
Dec 12th, 2015, 12:06 PM
#2
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.
-
Dec 12th, 2015, 12:18 PM
#3
Thread Starter
Lively Member
Re: RichTextBox Extended Find & Select
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.
-
Dec 12th, 2015, 12:32 PM
#4
Re: RichTextBox Extended Find & Select
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
-
Dec 12th, 2015, 01:12 PM
#5
Thread Starter
Lively Member
Re: RichTextBox Extended Find & Select
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|