c# - How to check string array contains a string

String array contains a string
string-array-contains-a-string.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        //this section create a string variable.
        string stringToSearch1 = "gg";
        string stringToSearch2 = "gr";
        string[] colors = { "red", "green", "blue", "yellow" };
        
        Label1.Text = "string to check/search/find..................<br />";
        Label1.Text += stringToSearch1;
        Label1.Text += "<br />" + stringToSearch2;

        Label1.Text += "<br /><br />string array elements..................";
        foreach (string s in colors)
        {
            Label1.Text += "<br />" + s;
        }

        //this line check string array any elements contains specific string
        Boolean result1 = colors.Any(x => x.Contains(stringToSearch1));
        Boolean result2 = colors.Any(x => x.Contains(stringToSearch2));

        Label1.Text += "<br /><br />string array contains string(gg)?...........";
        Label1.Text += result1.ToString();

        Label1.Text += "<br /><br />string array contains string(gr)?...........";
        Label1.Text += result2.ToString();
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string array contains a string</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string array contains a string
        </h2>  
        <hr width="550" align="left" color="Gainsboro" />  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"
            >  
        </asp:Label>  
        <br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="string array contains a string"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples