C# Program to search for a string in an array of strings

CsharpProgrammingServer Side Programming

Use Linq Contains() method to search for as specific string in an array of strings.

string[] arr = { "Bag", "Pen", "Pencil"};

Now, add the string in a string variable i.e. the string you want to search.

string str = "Pen";

Use the Contains() method to search the above string.

arr.AsQueryable().Contains(str);

Let us see the entire example.

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
   static void Main() {
      string[] arr = { "Bag", "Pen", "Pencil"};
      string str = "Pen";
      bool res = arr.AsQueryable().Contains(str);
      Console.WriteLine("String Pen is in the array? "+res);
   }
}

Output

String Pen is in the array? True
raja
Published on 14-Sep-2018 13:07:37
Advertisements