【问题标题】:how to split the string between two strings in c#?如何在c#中将字符串拆分为两个字符串?
【发布时间】:2012-12-14 18:04:37
【问题描述】:

我有一个包含 HTML 数据的字符串变量。现在我想将该 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. 根据&lt;/p&gt;标签将HTML字符串拆分成token。
  2. 并获取第一个令牌并将其存储在单独的字符串变量(firstPara)中。
  3. 现在获取每个标记,然后删除所有以&lt;p 开头和以&lt;/p&gt; 结尾的标记。并将每个值存储在单独的变量中。

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

5.所以,现在变量 firstPara 具有整个值...

  1. 最后,我们只需在 firstPara 的末尾附加 &lt;/p&gt;...

这是我的问题...

你能帮我解决这个问题吗...

【问题讨论】:

  • 需要解析html时使用Html Agility Pack
  • @TimSchmelter:感谢您为我推荐 Html Agility 包...今天我学到了一个非常有用的...

标签: c# string c#-4.0 string-parsing


【解决方案1】:

这里是正则表达式示例如何做到这一点。

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

【讨论】:

  • 不应该 &lt;p.*\&gt;&lt;p[^&gt;]*&gt;
  • 同理,正则表达式默认使用贪婪搜索
  • 我知道,我只是没有意识到lookaroundsatomic,所以我今天学到了一些新东西。谢谢! :)
【解决方案2】:

如果您想将一个string 拆分为另一个string,您可以使用string.Split(string[] separator, StringSplitOptions options),其中separator 是一个string 数组,其中包含至少一个用于拆分@987654326 的字符串@

示例

//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 字符,所以它无法读取 स्द्स्द्सद्स्द。因此,它被翻译为??????????????

谢谢,
希望对您有所帮助:)

【讨论】:

  • @Saravanan 对不起,关于那个,但我不能真正理解你的评论。能否请您提供您到底想要做什么? :)
  • 我想替换

    标记。这意味着我只有一个

    将位于 html 字符串起始位置和一个

    标记将位于 html 字符串结束位置.不在任何其他地方...
  • @Saravanan 我已经更新了我的答案。抱歉,误解了。祝你有美好的一天:)
猜你喜欢
  • 2011-06-14
  • 2011-02-01
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-15
  • 2017-07-13
  • 1970-01-01
相关资源
最近更新 更多