Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

How do I split up my string into substrings based on key charactors.

How do I break up my string into substrings... Lets say using "<!---->"

Start:
string myString = "<DIV class=ftrimage><A href=pic.asp><IMG src=168.jpg width=104></A></DIV><!---->Bush
vows steps to bolster al-Maliki's ability to curb sectarian bloodshed.<!----><A href=pic.asp><IMG src=168.jpg width=104></A>";

Results:
myString1 = "<DIV class=ftrimage><A href=pic.asp><IMG src=168.jpg width=104></A></DIV>";
myString2 = "Bush vows steps to bolster al-Maliki's ability to curb sectarian bloodshed.";
myString3 = "<A href=pic.asp><IMG src=168.jpg width=104></A>";
0
quest_capital
Asked:
quest_capital
1 Solution
 
Fernando SotoCommented:
Hi quest_capital;

Here is a solution using regular expression.

using System.Text.RegularExpressions;

            string myString = @"<DIV class=ftrimage><A href=pic.asp><IMG src=168.jpg width=104></A></DIV><!---->Bush
vows steps to bolster al-Maliki's ability to curb sectarian bloodshed.<!----><A href=pic.asp><IMG src=168.jpg width=104></A>";

            MatchCollection mc = Regex.Matches(myString, "(?:(.*?)(?:<!---->|$))",
                RegexOptions.Singleline);
            string[] output = new string[mc.Count];
            int idx = 0;

            foreach( Match m in mc )
            {
                output[idx] = m.Groups[1].Value;
                idx++;
            }

            // Print the array out
            foreach (string line in output)
            {
                if( line != "" ) Console.WriteLine(line + "\n");
            }


Fernando
0
 
ozymandiasCommented:
Or, if you are using .NET 2.0 you can do :

string[] values = myString.Split("<!---->");

myString1 = values[0];
myString2 = values[1];
myString3 = values[2];
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now