简体   繁体   中英

part of String contained in a List

Here's what I'm trying to do:

  1. Create a list with some values from mysql.
  2. Search this list with a variable ( I named it Existed )
  3. If Existed contains a specific string, then do some actions.

Here's a sample of my list data:


Facebook
Google
Yahoo
Strongman
Zombies
Stratovarius

If Existed inside users contains Strong, then perform some action.

My code so far is below. The problem is that it never enters the action and for some reason I believe it does not see " Strong " right.

List<string> users = dbm.FindManagers();
foreach (var Existed in users)
{
    if (Existed.Contains(rName_Add_User_result))
    {
        dbm.AddSubuser(Existed, rName_result);
    }
}

Can't reproduce. This works for me:

var rName_Add_User_result = " Strong ";
//List<string> users = dbm.FindManagers();
var users = new List<string>() {"Facebook", "Google", "Yahoo", "Strongman", "Zombies", "Stratovarius"};

foreach (var Existed in users.Where(u => u.ToUpper().Contains(rName_Add_User_result.ToUpper().Trim()))
{
     //dbm.AddSubuser(Existed, rName_result);
     Console.WriteLine(Existed);
}

Result:

Strongman

Not sure but could be because of case sensitivity. Try converting it to lower and then compare

if (Existed.ToLower().Contains(rName_Add_User_result))
{ 
    dbm.AddSubuser(Existed, rName_result);
}

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