|
-
Sep 7th, 2010, 12:24 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Deleting all but a certain part of TextBox.Text
I've done numerous searches, but can't find exactly what I'm looking for. I have a TextBox1 that contains a String in the format:
Code:
repairhelp&poster=1015857078&time=1283836408&bay=230059&src=wp_repairhelp&gs=wp_repairhelp&ref=nf
The numbers are random - I am trying to remove all the text except for:
[ Which is always a random number ] - I need to have the TextBox1.Text = Just the number after poster= - How difficult is this to achieve?
-
Sep 7th, 2010, 01:09 AM
#2
Hyperactive Member
Re: Deleting all but a certain part of TextBox.Text
You need a TextBox txtSomeTextBox and a button btnRemoveTextPartRegex on your form
VB.Net Code:
Private Sub btnRemoveTextPartRegex_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveTextPartRegex.Click
'Option A RESULT: poster=1015857078
Dim srgx As New System.Text.RegularExpressions.Regex(".*&(poster=\d*)&.*") 'regex with 1 capture group: (.*)
'Option B RESULT: 1015857078
'Dim srgx As New System.Text.RegularExpressions.Regex(".*&poster=(\d*)&.*") 'regex with 1 capture group: (.*)
Dim tmpstring As String = txtSomeTextBox.Text 'text you need to parse
Dim matchG As System.Text.RegularExpressions.MatchCollection = srgx.Matches(tmpstring) 'extract it
txtSomeTextBox.Text = matchG(0).Groups(1).ToString()
End Sub
-
Sep 7th, 2010, 03:28 AM
#3
Fanatic Member
Re: Deleting all but a certain part of TextBox.Text
Assuming some parts of the text are static, you can edit this slightly to your needs (make sure you only get indexes of parts of the string that will remain constant). There are probably better ways of doing this but if you want an alternative to regex, here you go:
vb Code:
Private Function SplitThis(ByVal WhatToSplit As String, ByVal between1 As String, ByVal between2 As String)
Dim str As String = ""
Dim index1 As Integer = WhatToSplit.IndexOf(between1)
Dim index2 As Integer = WhatToSplit.IndexOf(between2)
str = WhatToSplit.Substring(index1, index2 - index1)
Return str
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(SplitThis(TextBox1.Text, "poster=", "&time"))
End Sub
In this case you're reading between two points, you're instructing it to start reading at the first index of "poster=" and stop as soon as it reaches the first index of "&time" in this case it will get
Oh yeah, textbox1.text just contained
Code:
repairhelp&poster=1015857078&time=1283836408&bay=230059&src=wp_repairhelp&gs=wp_repairhelp&ref=nf
Hope that helps you a bit.
-
Sep 7th, 2010, 09:03 AM
#4
Thread Starter
Addicted Member
Re: Deleting all but a certain part of TextBox.Text
Zeljko, that worked perfectly - Thank you!
Last edited by relentlesstech; Sep 7th, 2010 at 09:07 AM.
-
Sep 7th, 2010, 09:45 AM
#5
Hyperactive Member
Re: [RESOLVED] Deleting all but a certain part of TextBox.Text
np, glad to help, thanks
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
|