Finding whether the list contains a word with the letter 'r' in it - CSharp LINQ

CSharp examples for LINQ:IEnumerable

Description

Finding whether the list contains a word with the letter 'r' in it

Demo Code




using System;/*from  w  w w .  jav a 2s.co m*/
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
        int[] numArray = numbers.ToArray();
        List<string> words = new List<string> { "one", "two", "three", "four", "five" };

        bool hasWordContainingR = words.Exists(word => word.Contains("r"));
        Console.WriteLine("\tIs there a word containing an 'r'? {0}", hasWordContainingR.ToString());
        if (hasWordContainingR)
        {
            string theWord = words.Find(word => word.Contains("r"));
            if (!String.IsNullOrEmpty(theWord)) Console.WriteLine("\tThe word is {0}", theWord);
        }

    }
}

Result


Related Tutorials