Reputation: 20078
edit: the order might change as you can see in the below example, both string have same name but different order....
How would you go after checking to see if the both string array match?
the below code returns true but in a reality its should return false since I have extra string array in the _check
what i am trying to achieve is to check to see if both string array have same number of strings.
string _exists = "Adults,Men,Women,Boys";
string _check = "Men,Women,Boys,Adults,fail";
if (_exists.All(s => _check.Contains(s))) //tried Equal
{
return true;
}
else
{
return false;
}
Upvotes: 1
Views: 1664
Reputation: 57220
Those are not array of strings, but two strings.
So, you actually need to split them into substrings before checking for the content equality.
You can do in this way:
string _exists = "Adults,Men,Women,Boys";
string _check = "Men,Women,Boys,Adults,fail";
var checks = _check.Split(',');
var exists = _exists.Split(',');
bool stringsEqual = checks.OrderBy(x => x).SequenceEqual(exists.OrderBy(x => x));
To speed up a bit some special cases, you could check for length before calling the LINQ code (avoiding the two OrderBy's in case of different lengths). Furthermore, to save memory, you could use in-place sort on the splits arrays, i.e. :
string _exists = "Adults,Men,Women,Boys";
string _check = "Men,Women,Boys,Adults,fail";
var checks = _check.Split(',');
var exists = _exists.Split(',');
if(checks.Length != exists.Length)
return false;
Array.Sort(checks);
Array.Sort(exists);
if (checks.SequenceEqual(exists))
return true;
return false;
Obviously these optimizations are useful only if your strings are really long, otherwise you can simply go with the LINQ one-liner.
Upvotes: 2
Reputation: 16936
First of all you need to split the strings to get arrays and sort them
var ary1 = _existing.Split(',').Trim().OrderBy(x => x);
var ary2 = _check.Split(',').Trim().OrderBy(x => x);
Now you can use 'SequenceEquals' to compare the Enumerables
var result = ary1.SequenceEquals(ary2);
SeqenceEquals compares the position and value, so if you want to detect positional changes as well, remoce the OrderBy.
Upvotes: 0
Reputation: 1221
If you just want to count strings try:
bool sameAmountOfStrings = _exists.Count(c => c.Equals(',')) == _check.Count(c => c.Equals(,));
Upvotes: 0
Reputation: 116188
string _exists = "Adults,Men,Women,Boys";
string _check = "Men,Women,Boys,Adults,fail";
bool b = _exists.Split(',').OrderBy(s=>s)
.SequenceEqual(_check.Split(',').OrderBy(s=>s));
Upvotes: 3
Reputation: 2921
Split the strings to make two list, and later compare them using Linq to Objects
string _exists = "Adults,Men,Women,Boys";
string _check = "Men,Women,Boys,Adults,fail";
List<string> exists = new List<string>(_exists.Split(new char[] { ',' }));
List<string> check = new List<string>(_check.Split(new char[] { ',' }));
foreach(string toCheck in check){
if(exists.Contains(toCheck)){
//do things
}
}
Upvotes: 0
Reputation: 2201
If you want to see if the number of substrings separated by a comma is the same, then use this.
public bool StringsHaveSameNumberOfSubstring(string _exists, string _check)
{
return (_exists.Split(',').Length == _check.Split(',').Length);
}
This is what I understood from your question.
Upvotes: 0
Reputation: 42390
try
return (_exists.Length == _check.Length);
That will check if the string arrays are the same length, but not necessarily the same values.
If you want to compare the arrays to see if they are exactly the same you will need to do the above first, then most likely sort the arrays into A-Z order, and compare each element
NOTE: This is unnecessary...
if (_exists.All(s => _check.Contains(s))) //tried Equal
{
return true;
}
else
{
return false;
}
...you can do this, and it's more elegant...
return (_exists.All(s => _check.Contains(s)));
Upvotes: 0