在Object中连接字符串的最佳方法

时间:2013-01-07 19:52:38

标签: c# html-agility-pack

在Object(HTMLcollection)中加入字符串的最佳方法? 我使用带有HTMLagilityPack的microsoft studio 2008,NET 3.5

这里的HTML

<div class="content">
<ul>
    <li>Text that I want to scrape </li>
    <li>Text that I want to scrape </li>
    <li>Text that I want to scrape </li>
</ul>
</div>  

这里是我的代码

var productfeature = 
    document.DocumentNode.SelectNodes("//div[@class='content']/ul/li");

if (productfeature != null)
{
    StringBuilder sb = new StringBuilder();
    foreach (HtmlNode node in productfeature)
    {
        //Make sure nothing {} inside title
        string safeint = node.InnerText.Replace("}", "").Replace("{",""); 
        sb.Append(safeint + "}"); //adding } for marking 
    }
}

这里有一篇文章说,使用 string.join 更好,但是我不知道怎么用对象元素

注意:我想要更快更轻松的东西..

1 个答案:

答案 0 :(得分:1)

以下是使用string.Join

的实现
string delimiter = safeint + "}";
string result = "{" + string.Join(delimiter,
    productfeature.Select(node => removeBraces(node.InnerText)).ToArray()) + "}";

public static string removeBraces(string value)
{
    return value.Replace("}", "").Replace("{", "");
}