开发者

C#: How do I find the index of an item when only part of the item name is known

Firstly, sorry if the title sounds confusing.

How do I find the index of an item in a list string when only a substring of that item is known?

Like, I have a list called directories. It has C:\test, C:\new and C:\files (3 items).

Using the word "new" only, how can I find the ind开发者_JAVA百科ex number of C:\new (that is 1) in directories?

I am using .NET Framework 4.0, if that matters.

Thanks in advance.


try this

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

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


You can try something like

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜