Check string array contains a value
The following asp.net c# example code demonstrate us how can we determine whether a sting array contains a
specified value programmatically at run time in an asp.net application. String array represent an array of
string data type values.
.Net framework's Enumerable.Contains() method exists in System.Linq namespace. Enumerable.Contains() method allow us to determine whether a sequence contains a specified element. So, we can check whether a specified element exists in an array by using this Contains() method.
We just need to specify the source array object to search specified element within it and we also need to pass the specified element value as parameter of Contains() method to search this element within the entire array. Contains() method return 'true' if it found the specified element in string array; otherwise it return 'false'.
.Net framework's Enumerable.Contains() method exists in System.Linq namespace. Enumerable.Contains() method allow us to determine whether a sequence contains a specified element. So, we can check whether a specified element exists in an array by using this Contains() method.
We just need to specify the source array object to search specified element within it and we also need to pass the specified element value as parameter of Contains() method to search this element within the entire array. Contains() method return 'true' if it found the specified element in string array; otherwise it return 'false'.
check-string-array-contains-a-value.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
string[] birds = new string[]
{
"Eurasian Bittern",
"Little Bittern",
"Striated Heron",
"Goliath Heron",
"Australian Pelican"
};
Label1.Text = "birds array.........<br />";
foreach (string s in birds)
{
Label1.Text += s + "<br />";
}
Boolean result = birds.Contains("Australian Pelican");
Label1.Text += "<br />'Australian Pelican' bird contains in birds array? " + result;
Boolean result2 = birds.Contains("Pygmy Cormorant");
Label1.Text += "<br /><br />'Pygmy Cormorant' bird contains in birds array? " + result2;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - check string array contains a value</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:DarkBlue; font-style:italic;">
c# example - check string array contains a value
</h2>
<hr width="550" align="left" color="LightBlue" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="X-Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="check string array contains a value"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to convert a byte array to a string
- How to copy elements in one array to another array
- How to perform binary search on an array
- How to delete an element from an array
- Find index of last element from an array that match the conditions
- How to filter an array elements
- How to get the length of an array