【问题标题】:C# HTML string -> get length without htmlC# HTML 字符串 -> 获取没有 html 的长度
【发布时间】:2016-08-24 12:31:49
【问题描述】:

我有带有 HTML 图像的字符串,例如:

string str = "There is some nice <img alt='img1' src='img/img1.png' /> images in this <img alt='img2' src='img/img2.png' /> string. I would like to ask you <img alt='img3' src='img/img3.png' /> how Can I can I get the Lenght of the string?";

我想得到没有图像和图像数量的字符串的长度。所以,结果应该是:

int strLenght = 111;
int imagesCount= 3;

你能告诉我最有效的方法吗?

谢谢

【问题讨论】:

  • 你可以在正则表达式的帮助下做到这一点。如果您需要基于它的解决方案,请告诉我
  • 看看这个答案来删除 HTML 标签:stackoverflow.com/a/18154046/5119765 然后你就可以得到字符串长度了。
  • 您最好的选择是使用像Html Agility Pack 这样的html解析器,这样您就可以正确计算内容的字符长度和图像标签的数量。

标签: c# html string image


【解决方案1】:

我建议使用真正的 HTML 解析器,例如 HtmlAgilityPack。那么就很简单了:

string html = "There is some nice <img alt='img1' src='img/img1.png' /> images in this <img alt='img2' src='img/img2.png' /> string. I would like to ask you <img alt='img3' src='img/img3.png' /> how Can I can I get the Lenght of the string?";

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
int length = doc.DocumentNode.InnerText.Length;               // 114
int imageCount = doc.DocumentNode.Descendants("img").Count(); // 3

这是DocumentNode.InnerText 在您的示例中返回的内容,您已经跳过了一些空格:

There is some nice  images in this  string. I would like to ask you  how Can I can I get the Lenght of the string?

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,我创建了这个方法。 您可以使用它来去除 HTML 标签并计算您的字符串

    public static string StripHtmlTags(string source)
    {
      if (string.IsNullOrEmpty(source))
      {
        return string.Empty;
      }
    
      var array = new char[source.Length];
      int arrayIndex = 0;
      bool inside = false;
      for (int i = 0; i < source.Length; i++)
      {
        char let = source[i];
        if (let == '<')
        {
          inside = true;
          continue;
        }
    
        if (let == '>')
        {
          inside = false;
          continue;
        }
    
        if (!inside)
        {
          array[arrayIndex] = let;
          arrayIndex++;
        }
      }
    
      return new string(array, 0, arrayIndex);
    }
    

    你的计数是这样的:

    int strLength = StripHtmlTags(str).Count;
    

    【讨论】:

    • 你知道你可以只做foreach(char let in source),因为string实现了IEnumerable&lt;char&gt;
    【解决方案3】:

    添加对 MSHTML(Microsoft HTML 对象库)的 (COM) 引用,您可以:

    var doc = (IHTMLDocument2)new HTMLDocument();
    doc.write(str);
    
    Console.WriteLine("Length: {0}", doc.body.innerText.Length);
    Console.WriteLine("Images: {0}", doc.images.length);
    

    【讨论】:

      【解决方案4】:

      如果您想在我上面的评论中提到的正则表达式的帮助下进行操作。请使用以下代码

      var regex = new System.Text.RegularExpressions.Regex("<img[^>]*/>");
      var plainString = regex.Replace(str, ""); 
      
      // plainString.length will be string length without images
          var cnt = regex.Matches(str).Count; // cnt will be number of images
      

      【讨论】:

        【解决方案5】:

        我喜欢 John Smith 的解决方案,但是我必须在末尾添加 Trim() 以匹配 MS Word 结果。

        使用这个:

        return new string(array, 0, arrayIndex).Trim();
        

        【讨论】:

          猜你喜欢
          • 2016-03-27
          • 2014-12-09
          • 1970-01-01
          • 2020-04-07
          • 2011-05-04
          • 2013-05-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多