繁体   English   中英

如何在c#中的两个字符串之间拆分字符串?

[英]how to split the string between two strings in c#?

我有一个包含HTML数据的String变量。现在我想将该html字符串拆分成多个字符串,然后最终将这些字符串合并为单个字符串。

这是html字符串:

<p><span style="text-decoration: underline; color: #ff0000;"><strong>para1</strong></span></p>
<p style="text-align: center;"><strong><span style="color: #008000;">para2</span> स्द्स्द्सद्स्द para2 again<br /></strong></p>
<p style="text-align: left;"><strong><span style="color: #0000ff;">para3</span><br /></strong></p>

这是我的预期输出:

<p><span style="text-decoration: underline; color: #ff0000;"><strong>para1</strong></span><strong><span style="color: #008000;">para2</span>para2 again<br /></strong><strong><span style="color: #0000ff;">para3</span><br /></strong></p>

我的分裂逻辑如下:

  1. 根据</p>标记将HTML字符串拆分为令牌。
  2. 并获取第一个令牌并将其存储在单独的字符串变量(firstPara)中。
  3. 现在取出每个标记,然后删除任何以<p开头并以</p>结尾的标记。并将每个值存储在单独的变量中。

4.然后取名为firstPara的第一个令牌并替换标签</p> ,然后附加我们通过步骤3得到的每个令牌。

5.所以,现在变量firstPara具有全部价值......

  1. 最后,我们只是在firstPara的末尾附加</p> ...

这是我的问题......

能不能请我离开这个问题......

这是正则表达式的例子,如何做到这一点。

String pattern = @"(?<=<p.*>).*(?=</p>)";
var matches = Regex.Matches(text, pattern);
StringBuilder result = new StringBuilder();
result.Append("<p>");
foreach (Match match in matches)
{
    result.Append(match.Value);
}
result.Append("</p>");

这就是你应该如何使用Html Agility Pack

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(text);
var nodes = doc.DocumentNode.SelectNodes("//p");
StringBuilder result = new StringBuilder();
result.Append("<p>");
foreach (HtmlNode node in nodes)
{
    result.Append(node.InnerHtml);
}
result.Append("</p>");

如果你想分割一个string被另一个string ,可以使用string.Split(string[] separator, StringSplitOptions options) ,其中separator是一个string数组,它包含将被用于分割所述至少一个字符串string

//Initialize a string of name HTML as our HTML code
string HTML = "<p><span style=\"text-decoration: underline; color: #ff0000;\"><strong>para1</strong></span></p> <p style=\"text-align: center;\"><strong><span style=\"color: #008000;\">para2</span> स्द्स्द्सद्स्द para2 again<br /></strong></p> <p style=\"text-align: left;\"><strong><span style=\"color: #0000ff;\">para3</span><br /></strong></p>";
//Initialize a string array of name strSplit to split HTML with </p>
string[] strSplit = HTML.Split(new string[] { "</p>" }, StringSplitOptions.None);
//Initialize a string of name expectedOutput
string expectedOutput = "";
string stringToAppend = "";
//Initialize i as an int. Continue if i is less than strSplit.Length. Increment i by 1 each time you continue
for (int i = 0; i < strSplit.Length; i++)
{
    if (i >= 1) //Continue if the index is greater or equal to 1; from the second item to the last item
    {
        stringToAppend = strSplit[i].Replace("<p", "<"); //Replace <p by <
    }
    else //Otherwise
    {
        stringToAppend = strSplit[i]; //Don't change anything in the string
    }
    //Append strSplit[i] to expectedOutput
    expectedOutput += stringToAppend;
}
//Append </p> at the end of the string
expectedOutput += "</p>";
//Write the output to the Console
Console.WriteLine(expectedOutput);
Console.Read();

产量

<p><span style="text-decoration: underline; color: #ff0000;"><strong>para1</stro
ng></span> < style="text-align: center;"><strong><span style="color: #008000;">p
ara2</span> ?????????????? para2 again<br /></strong> < style="text-align: left;
"><strong><span style="color: #0000ff;">para3</span><br /></strong></p>

注意 :由于我的程序不支持Unicode字符,因此无法读取स्द्स्द्सद्स्द 因此,它被翻译为??????????????

谢谢,
我希望这个对你有用 :)

暂无
暂无

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

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