简体   繁体   中英

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 . 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.

if (myString.Contains("james")}
{
}
Regex RegX = new Regex("james"); // james or any of your regex string
if (RegX.IsMatch(DropDownList1.Text))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM