Results 1 to 5 of 5

Thread: [RESOLVED] Deleting all but a certain part of TextBox.Text

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2009
    Location
    West Warwick, RI
    Posts
    193

    Resolved [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:
    Code:
    poster=1015857078
    [ Which is always a random number ] - I need to have the TextBox1.Text = Just the number after poster= - How difficult is this to achieve?

  2. #2
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    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:
    1. Private Sub btnRemoveTextPartRegex_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveTextPartRegex.Click
    2.  
    3.         'Option A RESULT:  poster=1015857078
    4.         Dim srgx As New System.Text.RegularExpressions.Regex(".*&(poster=\d*)&.*")               'regex with 1 capture group: (.*)
    5.         'Option B RESULT:  1015857078
    6.         'Dim srgx As New System.Text.RegularExpressions.Regex(".*&poster=(\d*)&.*")               'regex with 1 capture group: (.*)
    7.  
    8.         Dim tmpstring As String = txtSomeTextBox.Text                                            'text you need to parse
    9.         Dim matchG As System.Text.RegularExpressions.MatchCollection = srgx.Matches(tmpstring)   'extract it
    10.         txtSomeTextBox.Text = matchG(0).Groups(1).ToString()
    11. End Sub
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  3. #3
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    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:
    1. Private Function SplitThis(ByVal WhatToSplit As String, ByVal between1 As String, ByVal between2 As String)
    2.         Dim str As String = ""
    3.         Dim index1 As Integer = WhatToSplit.IndexOf(between1)
    4.         Dim index2 As Integer = WhatToSplit.IndexOf(between2)
    5.  
    6.         str = WhatToSplit.Substring(index1, index2 - index1)
    7.  
    8.         Return str
    9.     End Function
    10.  
    11.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    12.         MsgBox(SplitThis(TextBox1.Text, "poster=", "&time"))
    13.     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

    Code:
    poster=1015857078
    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2009
    Location
    West Warwick, RI
    Posts
    193

    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.

  5. #5
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: [RESOLVED] Deleting all but a certain part of TextBox.Text

    np, glad to help, thanks
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


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