Solved

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

Posted on 2006-11-30
2
232 Views
Last Modified: 2010-04-16
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
Comment
Question by:quest_capital
2 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 18049792
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
 
LVL 15

Accepted Solution

by:
ozymandias earned 250 total points
ID: 18050072
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question