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.

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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements