比较两个使用C#的stringbuilders

Phi*_*lly 0 c# asp.net-3.5

嗨我有两个字符串构建器s1和s2.我正在使用逗号分隔符为该stringbuilder分配订单.我想比较两个字符串构建器.我想知道s1中的所有订单都在s2中.如果不是我想知道s2中缺少哪个订单.怎么实现呢?

if (!IsPostBack)
{
    int reccount = dsResult.Tables[0].Rows.Count;
    for (int count = 0; count < reccount;count++)
    {
        HashSet<string> arrayOrdId = new HashSet<string>();
        arrayOrdId.Add(dsResult.Tables[0].Rows[count][1].ToString());
        // arrayOrdId[count] = dsResult.Tables[0].Rows[count][1].ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 7

我建议不要使用StringBuilder它 - 相反,创建两个List<string> 或可能HashSet<string>建立订单ID,然后比较那些.如果由于其他原因需要创建字符串表示,请单独执行此操作.(我假设您的订单ID是字符串.如果不是,请使用相应类型的集合.)

目前尚不清楚订单是否重要,但如果不重要,HashSet<string>那么您应该使用什么.您可以轻松找到差异:

var missingFromX = y.Except(x);
var missingFromY = x.Except(y);
// Do whatever you want with those differences
Run Code Online (Sandbox Code Playgroud)

  • @ user203127那是因为每次循环迭代时你都在重新创建`HashSet <string>`; 将声明移到循环之外. (3认同)