检查两个逗号分隔的字符串是否相等(对于内容集) [英] Check whether two comma separated strings are equal (for Content set)

查看:960
本文介绍了检查两个逗号分隔的字符串是否相等(对于内容集)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have four string as listed below. Though they have different order of characters and different spacing after comma – they are considered to have same business value.

  1. 如何检查所有字符串是否相同(根据上述业务场景)?我有以下代码,但是逗号后空格时失败.
  2. (为此目的)比Enumerable.SequenceEqual更好的方法是什么?
  1. How do I check that all the strings are same (according to the business scenario explained above) ? I have following code but it fails in the case of space after comma.
  2. What is the better method (for this purpose) than Enumerable.SequenceEqual?

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

Note: "A,B" will be considered same as "B,A,B,A,B"

注意:我在.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忽略大小写敏感性如何在不区分大小写的模式下使用HashSet< string> .Contains()方法?

Use OrdianlIgnoreCase for ignoring case sensitvity How to use HashSet<string>.Contains() method in case -insensitive mode?

参考:

  1. 检查内容的最佳方法.NET中逗号分隔列表中的字符串?
  2. 比较两个列表< T>平等的对象,忽略顺序
  3. 为什么IEnumerable< T> .Select()在2种情况中的1种情况下起作用?无法从使用情况推断出
  4. 什么比较两个逗号分隔的字符串进行匹配的最短代码是什么?
  5. 将分隔符分开使用c#和linq将字符串转换成层次结构
  6. 使用LINQ计算两个字符串之间的匹配字符
  7. Usinq Linq选择用半逗号分隔的字符串中的项目吗?
  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?如果是这样,则应该使用集合.如果不是,则按顺序排序是合适的.

Would you consider A,B to be equal to B,A,B,A,B? If so, you should be using sets. If not, an ordered sequence is appropriate.

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

Now we know you actually want set equality:

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


如果在设置相等性后不愿意 ...


If we weren't after set equality...

要忽略空格,应修剪它们.例如:

To ignore the spaces, you should just trim them. For example:

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);

这篇关于检查两个逗号分隔的字符串是否相等(对于内容集)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆