views:

69

answers:

3

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 index number of C:\new (that is 1) in directories?

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

Thanks in advance.

+6  A: 

try this

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

        var idx = tst.FindIndex(x => x.Contains("new"));
Ramesh Vel
oh... its same as mine... i was little bit late.... +1
Pramodh
+2  A: 

You can try something like

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

I tried these codes and they work great.

Thanks!

Iceyoshi
then mark it answered.. :)
Ramesh Vel