[英]Check if string contains a specific value
I have a IF
statement which I would want to check if my DropDownList
contains a specific string
. 我有一个
IF
语句,我想检查我的DropDownList
包含特定的string
。 May I know how can I check? 我可以知道如何检查吗?
Currently I'm working on this statement: 目前,我正在处理以下语句:
if (DropDownList1.Text='%james%')
{
}
Thank you 谢谢
if (DropDownList1.SelectedItem.Text.Contains("james")
{
//...
}
If you need to ignore case, you can do something like: 如果您需要忽略大小写,可以执行以下操作:
bool contains = DropDownList1.SelectedItem.Text.IndexOf("james", StringComparison.OrdinalIgnoreCase) >= 0;
if (contains)
{
//...
}
try this, 尝试这个,
if (DropDownList1.Items.Contains(new ListItem("james")))
{
// ... code here
}
or 要么
if (DropDownList1.Items.FindByText("james") != null)
{
// ... code here
}
Use string.Contains to check if a string contains another. 使用string。包含检查一个字符串是否包含另一个字符串。
if (myString.Contains("james")}
{
}
Regex RegX = new Regex("james"); // james or any of your regex string
if (RegX.IsMatch(DropDownList1.Text))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.