Solved

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

Posted on 2006-11-30
2
213 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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
c# string 6 39
C# System Cannot Find File Specified Error 4 55
Need help with a query 6 54
How do I get the id from URL? 19 47
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

18 Experts available now in Live!

Get 1:1 Help Now