[英]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>
我的分裂邏輯如下:
</p>
標記將HTML字符串拆分為令牌。 <p
開頭並以</p>
結尾的標記。並將每個值存儲在單獨的變量中。 4.然后取名為firstPara的第一個令牌並替換標簽</p>
,然后附加我們通過步驟3得到的每個令牌。
5.所以,現在變量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.