【问题标题】:C#: Replacing the first plain character in HTML stringC#:替换 HTML 字符串中的第一个纯字符
【发布时间】:2014-06-26 15:21:44
【问题描述】:

我想要做的是用新标签将 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”字母已更改为&lt;span class=\"my-style\"&gt;h&lt;/span&gt;

有人可以帮我吗?

【问题讨论】:

  • 发布您的 HtmlAgilityPack 代码,以便人们提供帮助。如果您正在执行字符串替换/正则表达式 - 最好将其保密以避免投票。
  • @Alexei Levenkov 我不太了解你。
  • 你考虑过用javascript做吗?会容易很多。
  • 请为我写代码在 SO 上并不完全受欢迎,因为您可能已经注意到了。预计答案包含您尝试执行任务的代码。因此,由于处理 HTML 的正常方式是使用 HtmlAgilityPack(或者您选择的其他解析器),我建议您展示使用它的示例。事实上,您的代码可能正在使用 string.Replace 或 Regex.Replace,但此类代码在处理 HTML 时通常不受欢迎 - 因此在这种情况下,您可能只是尝试自己处理代码(而不是先获得反对票)并关闭问题。
  • @shay__ 我更喜欢用 C# 来做(虽然我相信如果你设法用 JS 来做的话,很容易把它改成 C#)

标签: c# html string replace


【解决方案1】:

您可以使用以下两种方法。先提取innertext的第一个词:

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>

【讨论】:

    【解决方案2】:

    :first-letter 选择器可以帮助你使用 CSS 做到这一点。 http://www.w3schools.com/cssref/sel_firstletter.asp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 1970-01-01
      • 2020-03-10
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多