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