Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无秩序地包含_C#_Linq_Contains - Fatal编程技术网

C# 无秩序地包含

C# 无秩序地包含,c#,linq,contains,C#,Linq,Contains,我希望使用一组字符搜索字符串列表,并希望查找匹配项,而不考虑顺序。例如,如果我的列表包含 List<string> testList = new List<string>() { "can", "rock", "bird" }; 我想能够搜索使用irb和有它返回鸟。我必须这样做很多次,所以我在寻找最有效的方法 您可以使用linq来实现这一点 List<string> testList = new List<string>() { "can", "

我希望使用一组字符搜索字符串列表,并希望查找匹配项,而不考虑顺序。例如,如果我的列表包含

List<string> testList = new List<string>() { "can", "rock", "bird" };

我想能够搜索使用irb和有它返回鸟。我必须这样做很多次,所以我在寻找最有效的方法

您可以使用linq来实现这一点

List<string> testList = new List<string>() { "can", "rock", "bird" };
var lst = testList.Where(x => x.ToUpperInvariant().Contains("IRD")).ToList();

确保还使用ToUpper比较大小写,并且要比较的字符串也使用大写

您可以使用linq来实现这一点

List<string> testList = new List<string>() { "can", "rock", "bird" };
var lst = testList.Where(x => x.ToUpperInvariant().Contains("IRD")).ToList();

确保还使用ToUpper比较大小写,并将要比较的字符串设置为大写。对于您的场景,您需要检查另一个单词列表中的每个单词字符

为此,您可以这样做:

    // Checks whether all character in word is present in another word
    Func<string, string, bool> isContain = (s1, s2) =>
    {
        int matchingLength = 0;
        foreach (var c2 in s2.ToCharArray())
        {
            foreach (var c1 in s1.ToCharArray())
            {
                if (c1 == c2)
                    ++matchingLength;
            }
        }

        // if matched length is equal to word length given, it would be assumed as matched
        return s2.Length == matchingLength;
    };

    List<string> testList = new List<string>() { "can", "rock", "bird" };
    string name = "irb";
    var fileredList = testList.Where(x => isContain(x, name));

对于您的场景,您需要检查另一个单词列表中单词的每个字符

为此,您可以这样做:

    // Checks whether all character in word is present in another word
    Func<string, string, bool> isContain = (s1, s2) =>
    {
        int matchingLength = 0;
        foreach (var c2 in s2.ToCharArray())
        {
            foreach (var c1 in s1.ToCharArray())
            {
                if (c1 == c2)
                    ++matchingLength;
            }
        }

        // if matched length is equal to word length given, it would be assumed as matched
        return s2.Length == matchingLength;
    };

    List<string> testList = new List<string>() { "can", "rock", "bird" };
    string name = "irb";
    var fileredList = testList.Where(x => isContain(x, name));
对于testList中的每个项,测试以查看它是否包含查询中的所有字母


对于testList中的每个项目,如果您不关心匹配重复项,则检查您正在搜索的序列中的所有字符是否都包含在单词中,则测试以查看它是否包含查询中的所有字母,这对谓词有帮助:

 "irb".Except("bird").Count() == 0
和整体状况:

  List<string> testList = new List<string>() { "can", "rock", "bird" };
  var search = "irb";
  var matches = testList.Where(word => !search.Except(word).Any());
注:

如果需要混合大小写字母匹配,则需要将所有单词规范化为小写。 若搜索不同值的性能非常关键—请首先将搜索字符串转换为哈希集,然后手动执行。 如果需要多次对同一列表匹配不同的值,请将字符串列表转换为哈希集列表,并使用search.Allc=>wordashset.Containsc作为条件。
如果您不关心匹配重复项,则检查您正在搜索的序列中的所有字符是否都包含在单词中,这对于谓词来说是可行的:

 "irb".Except("bird").Count() == 0
和整体状况:

  List<string> testList = new List<string>() { "can", "rock", "bird" };
  var search = "irb";
  var matches = testList.Where(word => !search.Except(word).Any());
注:

如果需要混合大小写字母匹配,则需要将所有单词规范化为小写。 若搜索不同值的性能非常关键—请首先将搜索字符串转换为哈希集,然后手动执行。 如果需要多次对同一列表匹配不同的值,请将字符串列表转换为哈希集列表,并使用search.Allc=>wordashset.Containsc作为条件。
仅当字母的顺序与搜索词中出现的字母的顺序相同时,才会找到。我想说输入或并带回Rock我想您必须使用成对搜索。@jsomers89匹配词的显示方式只有一件事。我的意思是如何匹配单词或单词也可以像rk一样出现?只有当字母与搜索词中出现的字母顺序相同时,才会找到。我想说输入或并带回Rock我想您必须使用成对搜索。@jsomers89匹配词的显示方式只有一件事。我的意思是如何匹配单词或单词也可以像rk一样出现?