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

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.


Let us see the entire 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);


String Pen is in the array? True
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know