Array Search in C#

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

Ebook Download
View all
Learn
View all