C#:当只知道部分项目名称时,如何找到项目的索引

时间:2010-10-13 04:50:00

标签: c# string indexing

首先,对不起,如果标题听起来有点混乱。

如果只知道该项目的子字符串,我如何在列表字符串中找到项目的索引?

就像,我有一个名为目录的列表。它有C:\ test,C:\ new和C:\ files(3项)。

仅使用“new”一词,如何在目录中找到C:\ new(即1)的索引号?

我正在使用.NET Framework 4.0,如果这很重要。

提前致谢。

2 个答案:

答案 0 :(得分:6)

试试这个

        List<string> tst = new List<string>() { @"C:\test", @"C:\new", @"C:\files" };

        var idx = tst.FindIndex(x => x.Contains("new"));

答案 1 :(得分:2)

您可以尝试类似

的内容
int i = new List<string>
                        {
                            @"C:\test", 
                            @"C:\new", 
                            @"C:\files"
                        }.FindIndex(0, x => x.Contains("new"));