Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.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中查找字符串位置#_C#_String_List - Fatal编程技术网

C# 在列表C中查找字符串位置#

C# 在列表C中查找字符串位置#,c#,string,list,C#,String,List,我有一个列表list strList,其中字符串在整个程序中添加到列表中 我希望能够找到包含特定字符串的列表的特定位置或索引 我已使用以下代码检查是否存在包含特定文本的字符串: if (strList.Exists(str => str.Contains("Chicken"))) { Console.WriteLine("Found it"); } 但是我想找出这个字符串在列表中的位置。您可以使用FindIn

我有一个列表
list strList
,其中字符串在整个程序中添加到列表中

我希望能够找到包含特定字符串的列表的特定位置或索引

我已使用以下代码检查是否存在包含特定文本的字符串:

if (strList.Exists(str => str.Contains("Chicken")))
            {
                Console.WriteLine("Found it");
            }

但是我想找出这个字符串在列表中的位置。

您可以使用
FindIndex

 int index = strList.FindIndex(str => str.Contains("Chicken"));
搜索与指定谓词定义的条件匹配的元素,并返回整个列表中第一次出现的从零开始的索引


Muhammad的解决方案对我来说很好,我只是需要避免案例问题,我使用了下面相同的代码,只是做了一些小改动

 int Index = listOfStrings.FindIndex(x => x.Msg.Equals(msg, StringComparison.OrdinalIgnoreCase));
可能重复的