Question

I have a string like

"<Canvas Background="#FF00FFFF" Name="Page_1" Width="1200" Height="900" ><TextBlock Name="PageTitle" /></Canvas><Canvas Background="#FF00FFFF" Name="Page_2" Width="1200" Height="900"><TextBlock Name="PageTitle" /></Canvas>"

I want to split this string into an array like

[< Canvas Background="#FF00FFFF" Name="Page_1" Width="1200" Height="900" >< TextBlock Name="PageTitle" />< /Canvas>],

[< Canvas Background="#FF00FFFF" Name="Page_2" Width="1200" Height="900">< TextBlock Name="PageTitle" />< /Canvas>]

But when i use

objectsAsStrings = contents.Split(new string[] { "/Canvas><Canvas" }, StringSplitOptions.None);

i get the delimeter removed, what i dont want. How do i Split a string BETWEEN "/Canvas" and "< Canvas" ?

Was it helpful?

Solution

try this

string mailstring = "<Canvas Background='#FF00FFFF' Name='Page_1' Width='1200' Height='900' ><TextBlock Name='PageTitle' /></Canvas><Canvas Background='#FF00FFFF' Name='Page_2' Width='1200' Height='900'<TextBlock Name='PageTitle' /></Canvas>";
            string splitor = "</Canvas>";
            string[] substrings = mailstring.Split(new string[] { splitor }, StringSplitOptions.None);
            string part1 = substrings[0] + splitor;
            string part2 = substrings[1] + splitor;

OTHER TIPS

Alternatively you can use an XML parser like so:

string xml = "<Canvas Background=\"#FF00FFFF\" Name=\"Page_1\" Width=\"1200\" Height=\"900\" ><TextBlock Name=\"PageTitle\" /></Canvas><Canvas Background=\"#FF00FFFF\" Name=\"Page_2\" Width=\"1200\" Height=\"900\"><TextBlock Name=\"PageTitle\" /></Canvas>";
xml = "<Outer>" + xml + "</Outer>";
XDocument doc = XDocument.Parse(xml);
string[] array = doc.Descendants("Canvas").Select(item => item.ToString(SaveOptions.DisableFormatting)).ToArray();

Then array[] will contain what you want. This might be more generally useful.

This gives you the opportunity to do some higher-level parsing like so (Console App code):

string xml = "<Canvas Background=\"#FF00FFFF\" Name=\"Page_1\" Width=\"1200\" Height=\"900\" ><TextBlock Name=\"PageTitle1\" /></Canvas><Canvas Background=\"#FF00FFFF\" Name=\"Page_2\" Width=\"1200\" Height=\"900\"><TextBlock Name=\"PageTitle2\" /></Canvas>";
xml = "<Outer>" + xml + "</Outer>";
XDocument doc = XDocument.Parse(xml);

var items = from item in doc.Descendants("Canvas") select new
{
    Background    = (string) item.Attribute("Background"),
    Name          = (string) item.Attribute("Name"),
    Width         = (int)    item.Attribute("Width"),
    Height        = (int)    item.Attribute("Height"),
    TextBlockName = (string) item.Element("TextBlock").Attribute("Name")
};

foreach (var item in items)
{
    Console.WriteLine
    (
        "Background = {0}\n" +
        "Name = {1}\n" +
        "Width = {2}\n" +
        "Height = {3}\n" +
        "TextBlockName = {4}\n",
        item.Background,
        item.Name,
        item.Width,
        item.Height,
        item.TextBlockName
    );
}

The output from this code is:

Background = #FF00FFFF
Name = Page_1
Width = 1200
Height = 900
TextBlockName = PageTitle1

Background = #FF00FFFF
Name = Page_2
Width = 1200
Height = 900
TextBlockName = PageTitle2

Use Split as you did, after that iterate over the array and insert "<Canvas" to the beginning of all items besides the first, append "/Canvas>" to the end of all items besides the last.

contents = "<Canvas Background=\"#FF00FFFF\" Name=\"Page_1\" Width=\"1200\" Height=\"900\" ><TextBlock Name=\"PageTitle\" /></Canvas><Canvas Background=\"#FF00FFFF\" Name=\"Page_2\" Width=\"1200\" Height=\"900\"><TextBlock Name=\"PageTitle\" /></Canvas>";
objectsAsStrings = contents.Split(new string[] { "/Canvas><Canvas" }, StringSplitOptions.None);

for(var i = 0; i < objectsAsStrings.Length; i++)
{
    if( 0 < i) objectsAsStrings[i] = "<Canvas" + objectsAsStrings[i];
    if( i < objectsAsStrings.Length-1) objectsAsStrings[i] = objectsAsStrings[i] + "/Canvas>";
}

@Najib is right

But you never know that how many substrings you gonna get......... hence

instead of

string part1 = substrings[0] + splitor;
string part2 = substrings[1] + splitor;

try this

for(int i=0;i<substrings.Lenght;i++)
{
    substrings[i]+=splitor;
}

Not sure if this is good to use linq for that string manipulation but here is another approach:

var contents = "<Canvas Background=\"#FF00FFFF\" Name=\"Page_1\" Width=\"1200\" Height=\"900\" ><TextBlock Name=\"PageTitle\" /></Canvas><Canvas Background=\"#FF00FFFF\" Name=\"Page_2\" Width=\"1200\" Height=\"900\"><TextBlock Name=\"PageTitle\" /></Canvas>";
var a = from word in contents.Split(new string[] { "/Canvas><Canvas" }, StringSplitOptions.None) select word;
var b = from word in a where word == a.First() select word.Replace("<Canvas","");
var c = from word in a where word == a.Last() select word.Replace("/Canvas>","");
var d = from word in a where word != a.Last() && word != a.First() select word;
var result = b.Concat(d).Concat(c).Select(f => "<Canvas" + f + "/Canvas>");`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top