3

I've a list defined as:

List<string> content = new List<string>(){ "Hello This", "This is", "This" };

I want a code to find if the list contains the Keyword This, and if yes get its first occurrence.

Existing Code :

foreach(string line in content){
     if(line.Contains("This"))
          return line;
}

I want to simply and know if some other alternative is there. If we know the complete string then we could use List.Contains, but for a substring, how to proceed?

USING .NET 2.0. Please suggest without using LINQ.

6
  • What's wrong with your current code? Commented May 10, 2016 at 9:56
  • @DGibbs nothing is wrong but I wanted to know if there's something more simple/direct I could be using instead of this. Commented May 10, 2016 at 9:57
  • 1
    @B V Raman It looks perfectly simple to me. If I couldn't use LINQ, that's how I'd do it. Only thing I'd suggest is making sure you're doing a case-insensitive comparison/contains Commented May 10, 2016 at 9:59
  • I think this is already the simplest solution without LINQ. Commented May 10, 2016 at 10:09
  • Which version of C#? Multiple answers use lambda expressions, but those didn't exist prior to C# 3, which was introduced after .NET 2.0. Commented May 10, 2016 at 10:22

4 Answers 4

2

As mentioned on MSDN, FindIndex is available since Framework 2.0 and can be used for your problem.

FindIndex searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire List.

List<string> content = new List<string>() { "Hello This", "This is", "This" };
var index = content.FindIndex(p => p.Contains("This"));
if (index >= 0)
    return content[index];
Sign up to request clarification or add additional context in comments.

Comments

1

Here is what you were searching for in C#2.0 :

List<string> content = new List<string>() { "Hello This", "This is", "This" };
string keyword = "This";
string element = content.Find(delegate(string s) { return s.Contains(keyword); });

Comments

0

Find() The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T Msdn

Find() is available from NET Framework 2.0

 List<string> content = new List<string>() { "Hello This", "This is", "This" };
 string firstOccurance = content.Find(g => g.Contains("This"));

Comments

-1

You could use linq, this will do the same as your for loop:

return content.FirstOrDefault(x => x.Contains("This"));

3 Comments

He will want FirstOrDefault as he doesn't know the list contains the item he is searching for...
Thanks, but any approach without LINQ since I'm on .NET 2.0
Carra - its linq "FirstOrDefault" and "Contains".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.