简体   繁体   English

如何检查字符串是否包含字符串数组中的字符串

[英]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 下面的代码返回true,但实际上它应该返回false,因为我在_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). 为了加快一些特殊情况,您可以在调用LINQ代码之前检查长度(如果长度不同,请避免使用两个OrderBy)。 Furthermore, to save memory, you could use in-place sort on the splits arrays, ie : 此外,为了节省内存,您可以对splits数组使用就地排序,即:

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. 显然,只有当字符串很长时,这些优化才有用,否则,您可以简单地使用LINQ单线。

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 如果要比较数组以查看它们是否完全相同,则需要首先执行上述操作,然后很可能将数组按AZ顺序排序,然后比较每个元素

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 将字符串拆分成两个列表,然后使用Linq与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 现在您可以使用“ SequenceEquals”来比较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. SeqenceEquals比较位置和值,因此,如果您还想检测位置变化,请删除OrderBy。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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