1

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. 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?

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

Note: I am using Visual Studio 2010 with .Net Framework 4

CODE

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

UPDATE:

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

REFERENCE:

  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
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418

1 Answers1

8

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.

EDIT: 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);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • "A,B" will be considered same as "B,A,B,A,B" – LCJ Nov 29 '12 at 16:51
  • it would have saved me answering late again. +1 – Jodrell Nov 29 '12 at 17:01
  • When I use the set code, I get following error - Error 1 The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly. – LCJ Nov 29 '12 at 17:12
  • @Lijo: Which version of C# are you using? I'd expect that to work with C# 4, but there's an easy fix - I'll edit it now. – Jon Skeet Nov 29 '12 at 17:17
  • @JonSkeet I am using Visual Studio 2010 with .Net Framework 4 – LCJ Nov 29 '12 at 17:24
  • 1
    @Lijo: Okay - looks like the fact that `Trim` is overloaded could have been the problem here. See if my edit fixes it though. – Jon Skeet Nov 29 '12 at 17:28