Results 1 to 10 of 10

Thread: How to split the string?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2010
    Posts
    309

    How to split the string?

    Hi guys,

    Can you please help me with my code as I have a trouble with the returned strings that I have extracted from my php source to add the strings in my listview.

    I'm reading the tags of mystrings1 and mystrings2 for each paragraph from the php source. I got the returned strings which it looks like this:

    PHP Code:
    <p id='mystrings1'>my strings</p> | <a href="http://xfvasfasfasfasf">Link</a> </td> | <a href="delete.php?id=0">Delete</a> </td> | <span id="mystrings2">Enabled</td

    I want the returned strings to be like this when I reads the tags of mystrings1 and mystrings2 while ignore the other tags.

    PHP Code:
    <p id='mystrings1'>my strings</p> | <span id="mystrings2">Enabled</td

    Here's the code:

    Code:
    Public Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
    	timer1.Enabled = False
    	timer1.Stop()
    
    	Try
    		Dim URL1 As String = "http://programmingtsite.elementfx.com/myscript.php?user=test&pass=test"
    		Dim request1 As HttpWebRequest = CType(WebRequest.Create(URL1), HttpWebRequest)
    		Dim response1 As HttpWebResponse = CType(request1.GetResponse(), HttpWebResponse)
    		Dim reader1 As New StreamReader(response1.GetResponseStream())
    		Dim str1 As String = reader1.ReadToEnd()
    		Dim pattern1 As String = "(<p id='mystrings1'>(.*?)</p>(.*?)<span id=\"test\">(.*?)</td>)"
    		Dim matches1 As MatchCollection = Regex.Matches(str1, pattern1)
    
    
    		For Each x1 As Match In matches1
    			Dim StrArr1() As String = x1.Value.ToString().Split()
    			MessageBox.Show(x1.ToString())
    			Dim item1 As New ListViewItem("", 1)
    			item1.SubItems.Add(x1.Value.ToString().Replace("<p id='mystrings2'>", "").Replace("</p>", ""))
    			listView1.Items.AddRange(New ListViewItem() {item1})
    			listView1.CheckBoxes = True
    
    
    			If x1.Value.Contains("Enabled") Then
    				item1.Checked = True
    			End If
    		Next x1
    	Catch ex As Exception
    	End Try
    End Sub

    Do you know how I can ignore the other tags when I get the returned strings of mystrings1 and mystrings2??

    Any advice would be much appriecated.

    Thanks,
    PillKilla

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to split the string?

    will the 2 elements you want always be 1st + last in the returned string?

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to split the string?

    if yes, split on "|":

    dim elements() as string = returnedString.split("|"c)

    1st element = elements.first
    last element = elements.last

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2010
    Posts
    309

    Re: How to split the string?

    yes, that is what i want to split the first and the last.

    however, this is invalid code:

    1st element = elements.first
    last element = elements.last


    And Split is not a member of returnedString.

    Any idea?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to split the string?

    it's pseudocode. you'll work it out...

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2010
    Posts
    309

    Re: How to split the string?

    I can only use x1.Split("|c"), but it said that 'Split' : is not a member of 'System::Text::RegularExpressions::Match'

    Any idea?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to split the string?

    vb Code:
    1. x1.value.tostring.Split("|"c)

    .first + .last are linq extensions

    *****edited*****

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2010
    Posts
    309

    Re: How to split the string?

    i have input the .first and .last element, but they are not a member of system.text.regularexpressions.match.

    Any idea?

  9. #9
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: How to split the string?

    Code:
    Dim fields = x1.Value.ToString().Split("|"c)
    Dim first = fields.First
    Dim last = fields.Last
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to split the string?

    yep.

    match is a match
    .split is a member of the string class
    + .first + .last are LINQ extensions to the iEnumerable class (which includes string arrays)

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