Array Search in C#

By Rohatash Kumar Oct 30, 2012
This blog defines How we can search an array of strings.

This blog defines How we can search an array of strings. Here we create an array of string. Array of string contains some name.

string[] strname = { "Monu""Mohan""satendra""jeetendra""Rohatash" };

Find() method -  search an element that match with the condition defined by the specified predicate and return the first occurrence within the entire array.

Contains method - This returns a value indicating whether the specified string occurs within this string.

Example

we search an array of strings for a name containing the letter h in array.

C# code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            string[] strname = { "Monu""Mohan""satendra""jeetendra""Rohatash" };

            string match = Array.Find(strname, ContainsA);

            Console.WriteLine("Search string is:" + match);

        }

        static bool ContainsA(string findname)

        {

            return findname.Contains("h");

        }

 

    }

}

 

Output

 

Array-search-in-Csharp.jpg

Rohatash Kumar

Normal 0 false false false EN-AU X-NONE X-NONE I am a Microsoft .NET software Developer and author and C# Corner MVP. I hold Masters degree i... Read more

COMMENT USING

PREMIUM SPONSORS

Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

Latest Blogs