拆分2个子字符串之间的字符串,而不删除分隔符

时间:2014-05-06 09:41:05

标签: c# wpf string delimiter

我有一个像

这样的字符串
"<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>"

我想将此字符串拆分为类似

的数组
[< 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);

我删除了分隔符,我不想要的东西。如何拆分字符串BETWEEN“/ Canvas”和“&lt; Canvas”?

5 个答案:

答案 0 :(得分:3)

试试这个

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;

答案 1 :(得分:0)

或者,您可以像这样使用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();

然后array[]将包含您想要的内容。这可能更普遍有用。

这使您有机会进行更高级别的解析(控制台应用程序代码):

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
    );
}

此代码的输出为:

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

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

答案 2 :(得分:0)

使用Split,然后迭代数组并将"<Canvas"插入除第一项之外的所有项目的开头,将"/Canvas>"追加到除了{1}}之外的所有项目的末尾最后。

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>";
}

答案 3 :(得分:0)

@Najib是对的

但是你永远不知道你会得到多少个子串......... 因此

而不是

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

试试这个

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

答案 4 :(得分:0)

不确定使用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>");`