【问题标题】:Get the first few words from a long summary(plain string or HTML)从长摘要中获取前几个单词(纯字符串或 HTML)
【发布时间】:2009-10-16 10:41:40
【问题描述】:

我想使用 c# 从单词(纯字符串或 html)的长摘要中获取前几个单词(100 或 200)。

我的要求是显示内容长摘要的简短描述(此内容可能包含html元素)。我能够检索纯字符串,但是当它是 html 时,元素在示例之间被剪切,我得到这样的

<span style="FONT-FAMILY: Trebuchet MS">Heading</span>
</H3><span style="FONT-FAMILY: Trebuchet MS">
<font style="FONT-SIZE: 15px;

但它应该返回带有完整 html 元素的字符串。

我有一个 Yahoo UI 编辑器来从用户那里获取内容,我正在将该文本传递给下面的方法以获取简短摘要,

public static string GetFirstFewWords(string input, int numberWords)
{
     if (input.Split(new char[] { ' ' }, 
           StringSplitOptions.RemoveEmptyEntries).Length > numberWords)
        {
            // Number of words we still want to display.
            int words = numberWords;
            // Loop through entire summary.
            for (int i = 0; i < input.Length; i++)
            {
                // Increment words on a space.
                if (input[i] == ' ')
                {
                    words--;
                }
                // If we have no more words to display, return the substring.
                if (words == 0)
                {
                    return input.Substring(0, i);
                }
            }
            return string.Empty;
        }
        else
        {
            return input;
        }
}

我正在尝试从用户那里获取文章内容并在列表页面上显示简短摘要。

【问题讨论】:

  • 你目前正在做什么来获取单词?

标签: c# .net asp.net string


【解决方案1】:

想过让Html Agility Pack 为您出价吗?

虽然不完美,但这里有一个可以(或多或少)实现您所追求的想法:

// retrieve a summary of html, with no less than 'max' words
string GetSummary(string html, int max)
{
    string summaryHtml = string.Empty;

    // load our html document
    HtmlDocument htmlDoc = new HtmlDocument();
    htmlDoc.LoadHtml(html);

    int wordCount = 0;


    foreach (var element in htmlDoc.DocumentNode.ChildNodes)
    {
        // inner text will strip out all html, and give us plain text
        string elementText = element.InnerText;

        // we split by space to get all the words in this element
        string[] elementWords = elementText.Split(new char[] { ' ' });

        // and if we haven't used too many words ...
        if (wordCount <= max)
        {
            // add the *outer* HTML (which will have proper 
            // html formatting for this fragment) to the summary
            summaryHtml += element.OuterHtml;

            wordCount += elementWords.Count() + 1;
        }
        else 
        { 
            break; 
        }
    }

    return summaryHtml;
}

【讨论】:

    【解决方案2】:

    两个选项:

      1234563 .

      pro:完全控制,并且能够准确地获得 N 个可见词。
      缺点:干净地实施有点棘手。
    1. 删掉单词,然后将损坏的 HTML 输入 HtmlAgilityPack(可以帮助修复损坏的 HTML 的免费下载),然后就可以了。

      亲:几乎不需要编码,经过验证的解决方案,可维护
      缺点:当你调用.Substring() 时,你仍然需要想办法不计算标签

    【讨论】:

      【解决方案3】:

      您应该将内容和标记分开。你能提供更多关于你想要做什么的信息吗? (例如,这个字符串来自哪里,为什么要这样做)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-30
        • 2016-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-25
        • 2016-05-21
        • 1970-01-01
        相关资源
        最近更新 更多