【问题标题】:Remove string before and after given string paragraph in C#在C#中给定字符串段落之前和之后删除字符串
【发布时间】:2021-06-13 13:18:52
【问题描述】:

我在这里有一个示例段落:

var story = "Once upon a time, there was a little girl named <i>Goldilocks.</i>
She went for a walk in the forest. Pretty soon, she came upon a house. <strong>She 
knocked and, when no one answered, </strong>she walked right in. At the table in the kitchen, 
there were three bowls of porridge. Goldilocks was hungry. She tasted the porridge from the first bowl.";

我想要做的是在某些选定字符之前和之后修剪或删除某些段落。此特定段落位于&lt;/i&gt; 之后和&lt;strong&gt; html 标记之前。到目前为止,我尝试使用Substring

story.Substring(story.IndexOf("<strong>"));

但它会删除 &lt;strong&gt; 之前的所有内容我想要达到的结果是这样的:

var story = "Once upon a time, there was a little girl named <i>Goldilocks.</i><strong>She 
knocked and, when no one answered, </strong>she walked right in. At the table in the kitchen, 
there were three bowls of porridge. Goldilocks was hungry. She tasted the porridge from the first bowl.";

没有She went for a walk in the forest. Pretty soon, she came upon a house.

我也试过了,

story = story.Substring(story.IndexOf("</i>")).Substring(0, story.LastIndexOf("<strong>" + 1);

但它不起作用。

有什么帮助吗?

【问题讨论】:

  • 为什么要这样做?你的实际目标是什么?

标签: c# string substring


【解决方案1】:

您可以使用正则表达式来实现您的组并进行高级替换。 试试这个:

Regex.Replace(story, "</i>.*?<strong>", "</i><strong>");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 2016-03-13
    • 2018-04-29
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多