String contains list of strings
string-contains-list-of-strings.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 stringValue = "red green blue yellow brown";
//this line create a generic list of string.
List<string> colors = new List<string> {"pink","orange","yellow","gray","maroon" };
Label1.Text = "string..................<br />";
Label1.Text += stringValue;
Label1.Text += "<br /><br />string list..................";
foreach (string s in colors)
{
Label1.Text += "<br />" + s;
}
Boolean result = colors.Any(x => stringValue.Contains(x));
Label1.Text += "<br /><br />string contains value that match string list any element?...........";
Label1.Text += result.ToString();
if (result==true)
{
string result2 = colors.First(x => stringValue.Contains(x));
Label1.Text += "<br />found this element in string: " + result2.ToString();
}
else {
Label1.Text += "<br />no list element found in string.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - string contains list of strings</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - string contains list of strings
</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 contains list of strings"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to check whether a string contains a specific character
- How to check whether a string contains at least one number
- How to check whether a string contains numbers only
- How to check whether a string contains any special character
- String contains multiple values
- How to check string array contains a string
- How to escape double quotes in a string
- How to escape characters within a string
- String endswith ignorecase
- How to check whether a string ends with a number