Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to search for a string in an array of strings
Searching for a string in an array of strings is a common programming task. C# provides several methods to accomplish this, with the most straightforward being the LINQ Contains() method. This method returns a boolean value indicating whether the specified string exists in the array.
Syntax
Following is the syntax for using Contains() method to search in an array −
array.Contains(searchString)
You can also use it with AsQueryable() for LINQ queries −
array.AsQueryable().Contains(searchString)
Using LINQ Contains() Method
The simplest approach is to use the LINQ Contains() method which returns true if the string is found, false otherwise −
Example
using System;
using System.Linq;
class Program {
static void Main() {
string[] arr = { "Bag", "Pen", "Pencil" };
string str = "Pen";
bool res = arr.Contains(str);
Console.WriteLine("String '" + str + "' is in the array? " + res);
// Search for a string that doesn't exist
string notFound = "Book";
bool res2 = arr.Contains(notFound);
Console.WriteLine("String '" + notFound + "' is in the array? " + res2);
}
}
The output of the above code is −
String 'Pen' is in the array? True String 'Book' is in the array? False
Using Array.IndexOf() Method
Another approach is using Array.IndexOf() which returns the index of the first occurrence, or -1 if not found −
Example
using System;
class Program {
static void Main() {
string[] arr = { "Apple", "Banana", "Cherry", "Date" };
string searchStr = "Cherry";
int index = Array.IndexOf(arr, searchStr);
if (index >= 0) {
Console.WriteLine("String '" + searchStr + "' found at index: " + index);
} else {
Console.WriteLine("String '" + searchStr + "' not found in the array");
}
// Search for non-existing string
string notFound = "Grape";
int index2 = Array.IndexOf(arr, notFound);
Console.WriteLine("Index of '" + notFound + "': " + index2);
}
}
The output of the above code is −
String 'Cherry' found at index: 2 Index of 'Grape': -1
Case-Insensitive Search
For case-insensitive searches, you can use LINQ with StringComparison options −
Example
using System;
using System.Linq;
class Program {
static void Main() {
string[] arr = { "Red", "Green", "Blue", "Yellow" };
string searchStr = "red"; // lowercase
// Case-sensitive search
bool caseSensitive = arr.Contains(searchStr);
Console.WriteLine("Case-sensitive search for '" + searchStr + "': " + caseSensitive);
// Case-insensitive search using Any() with StringComparison
bool caseInsensitive = arr.Any(s => string.Equals(s, searchStr, StringComparison.OrdinalIgnoreCase));
Console.WriteLine("Case-insensitive search for '" + searchStr + "': " + caseInsensitive);
}
}
The output of the above code is −
Case-sensitive search for 'red': False Case-insensitive search for 'red': True
Comparison of Search Methods
| Method | Return Type | Use Case |
|---|---|---|
| Contains() | bool | Simple existence check |
| Array.IndexOf() | int | Get position of element |
| Any() with StringComparison | bool | Case-insensitive search |
Conclusion
C# provides multiple ways to search for strings in arrays. Use Contains() for simple existence checks, Array.IndexOf() when you need the position, and LINQ methods with StringComparison for case-insensitive searches.
