Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.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#_String_List - Fatal编程技术网

C# 在字符串列表中查找子字符串

C# 在字符串列表中查找子字符串,c#,string,list,C#,String,List,我有一个字符串列表,并且添加了多个字符串。 l1是一个包含三个值的列表 List<string> l1 = new List<string> { "issue: there are multiple data errors ", "resolution: you can correct the data or /n raise an SR on this issue", "rootfix: you need to be contious w

我有一个字符串列表,并且添加了多个字符串。 l1是一个包含三个值的列表

List<string> l1 = new List<string> 
{
     "issue: there are multiple data errors ",
     "resolution: you can correct the data or /n raise an SR on this issue",
     "rootfix: you need to be contious while entering the data "
};
List l1=新列表
{
“问题:存在多个数据错误”,
“解决方案:您可以更正数据或/或就此问题提出SR”,
“rootfix:输入数据时需要连续”
};
现在我需要检查这个列表是否包含了所有三个关键字符串

List<string> keyWords = new List<string> { "isue", "resolution", "rootfix" };
List关键字=新列表{“isue”、“resolution”、“rootfix”};
我不认为我可以使用
Contains
操作,因为上面的字符串是列表字符串的子字符串

请告诉我是否有任何可用的功能/逻辑


如果所有三个子字符串都在列表中,那么我需要返回boolian

我不完全清楚您的示例是否在
列表中显示了三个单独的字符串,或者它们是否是一个字符串

在第一种情况下,在
列表中有三个单独的字符串,您可以使用如下内容:

bool isInList = l1.Any(x => x.StartsWith("issue")) &&
                l1.Any(x => x.StartsWith("resolution")) &&
                l1.Any(x => x.StartsWith("rootfix"));
如果在
l1
中找到所有匹配字符串,则返回
true

如果是第二种情况,并且一个字符串包含您描述的值:

string l1 = "{\"issue: there are multiple data errors \", \"resolution: you can correct the data or /n raise an SR on this issue\", \"rootfix: you need to be contious while entering the data \"}";
bool isInString = l1.Contains("\"issue") &&
                  l1.Contains("\"resolution") &&
                  l1.Contains("\"rootfix"))
这将返回
true
,其中所有三个名称显示在同一字符串中。

这应该可以:

var data = new []{"issue: there are multiple data errors ", "resolution: you can correct the data or /n raise an SR on this issue", "rootfix: you need to be contious while entering the data "};

bool flag = (
    data.Any(d => d.StartsWith("issue:")) &&
    data.Any(d => d.StartsWith("resolution:")) &&
    data.Any(d => d.StartsWith("rootfix:"))
    );
您可以检查关键字是否匹配可能会影响您的关键字的词集中的项

List<string> keyWords = new List<string>{"issue","resolution","rootfix"};

List<string> wordSet = new List<string>{"issue: there are multiple data errors ", "resolution: you can correct the data or /n raise an SR on this issue", "rootfix: you need to be contious while entering the data "};

bool result = keyWords.All(x => wordSet.Any(w => w.Contains(x)));
List关键字=新列表{“问题”、“解决”、“根修复”};
List wordSet=new List{“问题:存在多个数据错误”,“解决方案:您可以更正数据或/n就此问题提出SR”,“rootfix:您需要在输入数据时保持连续”};
bool result=keyWords.All(x=>wordSet.Any(w=>w.Contains(x));

对于一个人来说,由内而外阅读可能更容易

你能用LINQ吗?只要使用Where()和String.StartsWith()就可以实现这一点

        var list = new List<string> {
            "issue: test1",
            "resolution: test2",
            "rootfix: test3"
        };

        var issues = list.Where(x => x.StartsWith("issue"));

        foreach(var i in issues) {
            Console.WriteLine(i);
        }

        Console.Read();
var list=新列表{
“问题:test1”,
“决议:test2”,
“rootfix:test3”
};
var问题=列表,其中(x=>x.StartsWith(“问题”);
foreach(发行中的var i){
控制台写入线(i);
}
Console.Read();

输出:“问题:test1”

您可以使用
StartsWith()
->使用
包含
有什么问题?您可以在循环中使用它或使用
列表。Any()
@Aousafrashid contains需要检查完整的字符串。但是我必须只检查子字符串,并且有多个子字符串“我不认为我可以使用contains操作”,您可以但不能使用
IEnumerable.contains
方法,而是
String.contains
方法