简体   繁体   English

检查字符串列表是否包含字符串的确切部分

[英]Check if list of string contains exact part of string

I have a List of strings as follows,我有一个字符串列表如下,

 List<string> str = new List<string>() { "7, 31", "2, 4", "5, 6" };

My input will be something like "1, 2" or "1"我的输入将类似于“1、2”或“1”

Is there a way to compare if any item in the list of strings excatly matches with the input.有没有办法比较字符串列表中的任何项目是否与输入完全匹配。 In the above case it should return a false.在上述情况下,它应该返回一个 false。 But if my input was "31" it should give me a true or if my input was "7, 31", also it should give true.但是,如果我的输入是“31”,它应该给出真值,或者如果我的输入是“7、31”,它也应该给出真值。

I try this code but it always returns true.我尝试了这段代码,但它总是返回 true。 Please help.请帮忙。

        bool res = false;
        List<string> substrings = new List<string>() { "7, 31", "2, 4", "5, 6" };
        string input = "1"; //Because my input can be "1" or "1, 31" etc spltting it.

        var inputSplitted = input.Split(',');

        var plates = substrings.Select(x => x.Split(','));

        foreach (var item in plates)
        {
            if (item.Any() == inputSplitted.Any())
            {
                res = true;
            }
        }

        Console.WriteLine(res);

item and inputSplitted are arrays of string , so Any called on them is just checking if those arrays are not empty (ie if there are any strings in them) which always will evaluate to true for provided examples. iteminputSplitted是 arrays 的string ,所以对它们调用的Any只是检查那些 arrays 是否不为空(即它们中是否有任何字符串),对于提供的示例,它总是评估为true

If I understand the rule correctly - input should be either present as full string in the original collection or as part of split by comma original collection - you should compare strings in the collections. Quick fix would be using SequenceEqual with extra handling of one element inputs (also it seems that you need to call .Select(s => s.Trim()).ToArray() on both split results):如果我正确理解规则 - 输入应该作为完整字符串出现在原始集合中或作为逗号原始集合拆分的一部分 - 你应该比较 collections 中的字符串。快速修复将使用SequenceEqual对一个元素输入进行额外处理(似乎您还需要在两个拆分结果上调用.Select(s => s.Trim()).ToArray() ):

if (item.SequenceEqual(inputSplitted)
    || (inputSplitted.Length == 1 && item.Contains(inputSplitted.First())))
{
    res = true;
}

But in general it will result in poor performance if such searches will be performed multiple times.但一般来说,如果多次执行此类搜索,将导致性能不佳。 I would recommend using HashSet for such searching:我建议使用HashSet进行此类搜索:

var hash = substrings
    .SelectMany(s => s.Split(",").Select(s => s.Trim())) // flatten split strings
    .Concat(substrings) // and concatenate with original
    .ToHashSet();

var res = hash.Contains(input); // search in collection (should be prepared one time)

If order of items is not important (ie "7, 31" and "31, 7" are the same) you can rebuild search and input strings by splitting them and using OrderBy(s => s) on the split results before concatenating back.如果项目的顺序不重要(即“7, 31”和“31, 7”相同),您可以通过拆分它们并在拆分结果上使用OrderBy(s => s)来重建搜索和输入字符串,然后再连接回去.

Guru Stron has a great answer. Guru Stron 有一个很好的答案。 An easy, readable solution to your problem would be to get the user input, split it (and trim it), and do the same to the list of strings to check from, and then just do something like一个简单易读的问题解决方案是获取用户输入,将其拆分(并修剪),并对要检查的字符串列表执行相同的操作,然后执行类似

inputSplitted.All(s => plates.Contains(s));

Or with method group: inputSplitted.All(plates.Contains);或者使用方法组: inputSplitted.All(plates.Contains);

This assumes I understood you question correctly, and that input order etc doesn't matter.这假设我正确理解您的问题,并且输入顺序等无关紧要。 You just want to find out if each number the user wrote is in the list somewhere.你只是想知道用户写的每个数字是否在列表中的某个地方。

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

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