简体   繁体   English

在2个子字符串之间拆分字符串,而不删除定界符

[英]Split String between 2 substrings without removing delimiters

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" ? 如何在“ / Canvas”和“ <Canvas”之间分割字符串?

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;

Alternatively you can use an XML parser like so: 另外,您可以使用XML解析器,如下所示:

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. 然后array[]将包含您想要的内容。 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. 像您一样使用Split ,然后遍历数组,在除第一个项之外的所有项的开头插入"<Canvas" ,在除最后一项之外的所有项的末尾附加"/Canvas>"

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 @Najib是对的

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: 不确定使用linq进行字符串操作是否很好,但这是另一种方法:

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>");`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM