How to split the returned strings?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: How to split the returned strings?

  1. #1
    Join Date
    Aug 2007
    Posts
    448

    How to split the returned strings?

    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:
    System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e)
    {
        timer1->Enabled = false;
        timer1::Stop();
    
        try
        {
            String ^URL1 = "http://programmingtsite.elementfx.com/myscript.php?user=test&pass=test";
            HttpWebRequest ^request1 = safe_cast<HttpWebRequest^>(WebRequest::Create(URL1));
            HttpWebResponse ^response1 = safe_cast<HttpWebResponse^>(request1->GetResponse());
            StreamReader ^reader1 = gcnew StreamReader(response1->GetResponseStream());
            String ^str1 = reader1->ReadToEnd();
            String ^pattern1 = "(<p id='mystrings1'>(.*?)</p>(.*?)<span id=\\"test / ">(.*?)</td>)";
            MatchCollection ^matches1 = Regex::Matches(str1, pattern1);
    
            for each (Match ^x1 in matches1)
            {
                array<String^> ^StrArr1 = x1->Value->ToString()->Split();
                MessageBox::Show(x1->ToString());
                ListViewItem ^item1 = gcnew ListViewItem("", 1);
      
             
     item1->SubItems->Add(x1->Value->ToString()->Replace("<p
     id='mystrings2'>", "")->Replace("</p>", ""));
                listView1::Items->AddRange(gcnew array<ListViewItem^> {item1});
                listView1->CheckBoxes = true;
    
                if (x1->Value->Contains("Enabled"))
                {
                    item1->Checked = true;
                }
            }
        }
        catch (Exception ^ex)
        {
        }
    }

    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,
    Mark

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,594

    Re: How to split the returned strings?

    So you seem to have a problem with regexes. Then, once you already got that far, why not simply do it without them? Perhaps something like this:

    Code:
    // Test11.cpp: Hauptprojektdatei.
    
    #include "stdafx.h"
    
    using namespace System;
    using namespace System::Collections::Generic;
    
    String ^FilterTags(String ^strInput)
    {
      array<String ^> ^astrTags = strInput->Split('|');
      List<String ^> ^lstFilteredTags = gcnew List<String ^>;
      for each (String ^strTag in astrTags) {
        strTag = strTag->Trim();
        if (strTag->StartsWith("<p id='mystrings1'>") || strTag->StartsWith("<span id=\"mystrings2\">"))
          lstFilteredTags->Add(strTag);
      }
      return String::Join(" | ", lstFilteredTags);
    }
    
    int main(array<System::String ^> ^args)
    {
      String ^strInput = "<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>";
      Console::WriteLine("Input string: {0}", strInput);
      Console::WriteLine();
      Console::WriteLine("Output string: {0}", FilterTags(strInput));
      Console::WriteLine();
      Console::WriteLine("Hit <Enter> to continue...");
      Console::ReadLine();
      return 0;
    }
    Or, if your sample string is even closer to the real scenario, simply take the first and the last one. But if it actually would be so simple you probably wouldn't have asked...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Aug 2007
    Posts
    448

    Re: How to split the returned strings?

    Thanks Eri523, but your code seems to be a little bit mess. I have adjusted the code the one you have posted to be enaged with my code. It seems to be working right now.

    Code:
    for each (Match ^x1 in matches1)
    		{
    			array<String^> ^StrArr1 = x1->Value->ToString()->Split('|');
    			List<String ^> ^lstFilteredTags = gcnew List<String ^>;
    
    			for each (String ^strTag in StrArr1) 
    			{
    				strTag = strTag->Trim();
    				
    				if (strTag->StartsWith("<p id='mystrings1'>"))
    				{
    					lstFilteredTags->Add(strTag);
    					MessageBox::Show(strTag);
    					ListViewItem ^item1 = gcnew ListViewItem("images",1);
    					item1->SubItems->Add(strTag->ToString()->Replace("<p id='channels'>", "")->Replace("</p>", ""));
    					listView1->Items->AddRange(gcnew array<ListViewItem^> {item1});
    					listView1->CheckBoxes = true;
    
    					if (x1->Value->Contains("Enabled"))
    					{
    						item1->Checked = true;
    					}
    				}
    			}			
    		}
    In my listview, the item are added like this:

    Code:
    <p id='mystrings1'>my strings</p><p id="images"> <a href="images.php?id=1">Images</a></td>

    I want to split the strings after the </p> to get the return strings like this:

    Code:
    <p id='mystrings1'>my strings</p>

    Do you know how to do this using with my code?

    If you could modify the code that I have created, i would be grateful.

    Thanks,
    Mark

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,594

    Re: How to split the returned strings?

    Quote Originally Posted by mark103 View Post
    Thanks Eri523, but your code seems to be a little bit mess. I have adjusted the code the one you have posted to be enaged with my code. It seems to be working right now.
    I must admit that I only took a brief look at your code, since it looked too confusing and still seems to depend on lots of unknown preconditions. Instead I solely based my code on your snippets of the desired input and output strings, added the premise not to use regexes. At any rate, fine that it works now.

    In my listview, the item are added like this:

    Code:
    <p id='mystrings1'>my strings</p><p id="images"> <a href="images.php?id=1">Images</a></td>

    I want to split the strings after the </p> to get the return strings like this:

    Code:
    <p id='mystrings1'>my strings</p>

    Do you know how to do this using with my code?

    If you could modify the code that I have created, i would be grateful.
    Again I'll prefer to write a bit of code that does the outlined transformation from scratch. It's quite basic string processing stuff I'd say:

    Code:
    String ^ChopAfterParagraph(String ^str)
    {
      return str->Substring(0, str->IndexOf("</p>") + 4);
    }
    You probably won't have real problems incorporating that into your code, like you did it with what I posted in post #2.

    Note that this is absolutely minimalistic and completely lacks any error checking. It will return nonsense or throw an exception if the input string actually doesn't contain any </p> tag.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center