[英]C#: Replacing the first plain character in HTML string
我想做的是用一個新標簽將HTML字符串中的第一個字符替換為我自己的自定義樣式。 不幸的是,我無法以一般的方式來完成我所有的示例。
考慮下一個可能的HTML字符串:
string str1 = "hello world";
string str2 = "<p><div>hello</div> world <div>some text</div></p>";
string str3 = "<p>hello <span>world</span></p>";
string str4 = "<p><a href="#">h</a>hello world</p>";
string str5 = "<p>hello world <div>some text</div></p>";
結果應為:
str1 = "<span class=\"my-style\">h</span>ello world";
str2 = "<p><div><span class=\"my-style\">h</span>ello</div> world <div>some text</div></p>";
str3 = "<p><span class=\"my-style\">h</span>ello <span>world</span></p>";
str4 = "<p><a href="#'><span class=\"my-style\">h</span></a>hello world</p>";
str5 = "<p><span class=\"my-style\">h</span>ello world <div>some text</div></p>";
結果中的“ h”字母已更改為<span class=\\"my-style\\">h</span>
有人可以幫我嗎?
:first-letter選擇器可以幫助您使用CSS做到這一點。 http://www.w3schools.com/cssref/sel_firstletter.asp
您可以使用以下兩種方法。 首先提取內文的第一個單詞:
private static string ExtractHtmlInnerTextFirstWord(string htmlText)
{
//Match any Html tag (opening or closing tags)
// followed by any successive whitespaces
//consider the Html text as a single line
Regex regex = new Regex("(<.*?>\\s*)+", RegexOptions.Singleline);
// replace all html tags (and consequtive whitespaces) by spaces
// trim the first and last space
string resultText = regex.Replace(htmlText, " ").Trim().Split(' ').FirstOrDefault();
return resultText;
}
注意:致謝http://www.codeproject.com/Tips/477066/Extract-inner-text-from-HTML-using-Regex
然后,將第一個單詞替換為編輯后的值(該值也稱為ExtractHtmlInnerTextFirstWord
。
private static string ReplaceHtmlInnerText(string htmlText)
{
// Get first word.
string firstWord = ExtractHtmlInnerTextFirstWord(htmlText);
// Add span around first character of first word.
string replacedFirstWord = firstWord.Replace(firstWord[0].ToString(), "<span class=\"my-style\">" + firstWord[0] +"</span>");
// Replace only first occurrence of word.
var regex = new Regex(Regex.Escape(firstWord));
string replacedText = regex.Replace(htmlText, replacedFirstWord, 1);
return replacedText;
}
您可以使用以下方法調用該方法:
private static void Main(string[] args)
{
string str1 = "hello world";
string str2 = "<p><div>hello</div> world <div>some text</div></p>";
Console.WriteLine("Original: " + str1);
Console.WriteLine("Edited value: " + ReplaceHtmlInnerText(str1));
Console.WriteLine("Original: " + str2);
Console.WriteLine("Edited value: " + ReplaceHtmlInnerText(str2));
Console.Read();
}
輸出:
Original: hello world
Edited value: <span class="my-style">h</span>ello world
Original: <p><div>hello</div> world <div>some text</div></p>
Edited value: <p><div><span class="my-style">h</span>ello</div> world <div>some text</div></p>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.