
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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
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
- Related Articles
- Sort an array of strings according to string lengths in C++
- C++ Program to Search for an Element in a Binary Search Tree
- C++ program to search specific values in an array
- C++ program to search an element in a sorted rotated array
- C# Program to find the longest string from an array of strings using Lambda Expression
- C Program to Sort an array of names or strings
- C program to search an array element using Pointers.
- Recursive program to linearly search an element in a given array in C++
- JavaScript Program for Search an element in a sorted and rotated array
- Java Program for Search an element in a sorted and rotated array
- Write a Golang program to search an element in an array
- C Program to Reverse Array of Strings
- Swift Program to Search an Element in an Array
- Python Program to Search an element in an Array
- Java Program to write an array of strings to a file

Advertisements