简体   繁体   中英

How to check if string contains a string in string array

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

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, ie :

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.

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

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.

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
}
}

如果只想计算字符串,请尝试:

bool sameAmountOfStrings = _exists.Count(c => c.Equals(',')) == _check.Count(c => c.Equals(,));

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM