【问题标题】:Check whether two comma separated strings are equal (for Content set)检查两个逗号分隔的字符串是否相等(对于内容集)
【发布时间】:2012-12-12 14:27:12
【问题描述】:

我有四个字符串,如下所示。尽管它们有不同的字符顺序和逗号后不同的间距——但它们被认为具有same business value

  1. 如何检查所有字符串是否相同(根据上述业务场景)?我有以下代码,但在逗号后有空格的情况下会失败。
  2. 有什么比Enumerable.SequenceEqual更好的方法(为此)?

注意:“A,B”将被视为与“B,A,B,A,B”相同

注意:我使用 Visual Studio 2010.Net Framework 4

代码

  string firstString = "A,B,C";
  string secondString = "C,A,B";
  string thirdString = "A,B, C";
  string fourthString = "C, A,B";


  //Set 1 Test
  List<string> firstList = new List<string>(firstString.Split(','));
  List<string> secondLsit = new List<string>(secondString.Split(','));
  bool isStringsSame = Enumerable.SequenceEqual(firstList.OrderBy(t => t), secondLsit.OrderBy(t => t));
  Console.WriteLine(isStringsSame);


  //Set 2 Test
  List<string> thirdList = new List<string>(thirdString.Split(','));
  List<string> fourthList = new List<string>(fourthString.Split(','));
  bool isOtherStringsSame = Enumerable.SequenceEqual(thirdList.OrderBy(t => t), fourthList.OrderBy(t => t));
  Console.WriteLine(isOtherStringsSame);

  Console.ReadLine();

更新

使用OrdianlIgnoreCase 忽略大小写敏感How to use HashSet<string>.Contains() method in case -insensitive mode?

参考

  1. Best way to check for string in comma-delimited list with .NET?
  2. Compare two List<T> objects for equality, ignoring order
  3. Why does the IEnumerable<T>.Select() works in 1 of 2 cases ? Can not be inferred from usage
  4. What is the shortest code to compare two comma-separated strings for a match?
  5. Split a separated string into hierarchy using c# and linq
  6. Count matching characters between two strings using LINQ
  7. Usinq Linq to select items that is in a semi-comma separated string?
  8. Determine whether two or more objects in a list are equal according to some property

【问题讨论】:

  • 你认为 A,B 等于 B,A,B,A,B 吗?如果是这样,您应该使用集合。如果不是,则有序序列是合适的。
  • @JonSkeet "A,B" 将被视为与 "B,A,B,A,B" 相同

标签: c# linq


【解决方案1】:

你认为 A,B 等于 B,A,B,A,B 吗?如果是这样,您应该使用集合。如果不是,则有序序列是合适的。

编辑:现在我们知道您实际上想要设置相等性:

var set1 = new HashSet<string>(firstString.Split(',').Select(t => t.Trim()));
bool setsEqual = set1.SetEquals(secondString.Split(',').Select(t => t.Trim()));

如果我们在设置相等后不是...

要忽略空格,您应该修剪它们。例如:

var firstOrdered = firstString.Split(',')
                              .Select(t => t.Trim())
                              .OrderBy(t => t);
var secondOrdered = secondString.Split(',')
                                .Select(t => t.Trim())
                                .OrderBy(t => t); 
bool stringsAreEqual = firstOrdered.SequenceEqual(secondOrdered);

【讨论】:

  • "A,B" 将被视为与 "B,A,B,A,B" 相同
  • 这样我就不会再迟到了。 +1
  • 当我使用设置代码时,出现以下错误 - 错误 1 ​​方法 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' 无法从用法中推断出来。尝试明确指定类型参数。
  • @Lijo:您使用的是哪个版本的 C#?我希望它可以与 C# 4 一起使用,但有一个简单的修复方法 - 我现在将对其进行编辑。
  • @Lijo:好的 - 看起来Trim 过载的事实可能是这里的问题。看看我的编辑是否修复了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
  • 1970-01-01
  • 1970-01-01
  • 2015-10-30
相关资源
最近更新 更多