Solved

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

Posted on 2006-11-30
2
219 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 62

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ADO.NET ENTITY DATA MODEL 3 30
Syntax error 9 45
Data is not showing from images 15 37
In WPF / C# binding a local database in code behind 1 24
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now