Array and LINQ - Select strings start with specific character.

Linq is handy. Say I have an array of strings "Ape", "Ant", "Dog", "Cat", "Elephant". I want to select only the strings starts with 'A'.

Here is the simple code



namespace My_Samples
{
using System;
using System.Linq;

class Program
{
static void Main(string[] args)
{
string[] inputs = new string[5] { "Ape", "Ant", "Dog", "Cat", "Elephant" };

var result = from i in inputs
where i[0].Equals('A')
select i;

foreach (string s in result)
{
Console.WriteLine(s);
}

Console.ReadLine();
}
}
}

No comments:

Post a Comment