Link to home
Start Free TrialLog in
Avatar of quest_capital
quest_capital

asked on

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>";
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of ozymandias
ozymandias
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial