简体   繁体   中英

How to find a color in a string that contains part of another color

I am trying to find a color in a string that contains part of another color. ie Some string will contain Rose Gold and others will contain just Gold or sometimes the string will contain Black and other times Jet Black or Midnight Black. and when I use the CONTAINS keyword it works randomly.

Galaxy S8 64GB artic Silver                      Just get Silver
Galaxy S7 edge in Black Onyx 32GB (CPO)          Just get Black
Galaxy S7 edge in Silver Titanium 32GB (CPO)     Just gets Silver
Galaxy S7 edge in Blue Coral 32GB                Just gets Blue
Moto Z2 Play - Fine Gold                         Just gets Gold
iPad Mini 4 32GB in Space Gray                   I got Space Gray oddly enough
iPad Pro (9.7) 32GB in Rose Gold                 I got Rose Gold oddly enough
Galaxy Note 8 in Midnight Black                  I got Midnight Black oddly enough


 sProductDescription = Row["ProductDescription"].ToString().Trim().Replace("  ", " ");
 Products pa = db.Products.Single(p => p.ModelNumber == sModelNumber);
 foreach (string x in SharedFunctions.GetColors())
 {
   if (sProductDescription.ToUpper().Contains(x.ToUpper()))
   {
       pa.Color = x;
   }
 }

public static List<string> GetColors()
{
    List<string> lColors = new List<string>(); 
    lColors.Add("Artic Silver");
    lColors.Add("Black Leather");
    lColors.Add("Black Onyx");
    lColors.Add("Black Sapphire");
    lColors.Add("Black Slate");
    lColors.Add("Black");
    lColors.Add("Blue Coral");
    lColors.Add("Blue");
    lColors.Add("Carbon");
    lColors.Add("Fine Gold");
    lColors.Add("Gold Platinum");
    lColors.Add("Gold");
    lColors.Add("Gray");
    lColors.Add("Green");
    lColors.Add("Grey");
    lColors.Add("Jet Black");
    lColors.Add("Lunar Grey");
    lColors.Add("Midnight Black");
    lColors.Add("Orchid Gray");
    lColors.Add("Pearl");
    lColors.Add("Pink");
    lColors.Add("Platinum");
    lColors.Add("Rose Gold");
    lColors.Add("Silver Titanium");
    lColors.Add("Silver");
    lColors.Add("Slate");
    lColors.Add("Space Gray");
    lColors.Add("Sugar White");
    lColors.Add("Titan");
    lColors.Add("White");
    lColors.Add("Yellow");

    return lColors;
}

I'd make 2 word lists containing the colours; A basic and advanced list. Your first should contain words such as "Rose Gold" and "Titanium White" for example - essentially just all of the colours you intend to use that have 'names'. In your second list, put words that are just plain colours such as "Gold", "Green" or "Silver". Have the program first iterate through your complex colour names, and then your simple one if no match was found in the first one.

var complexList = ColourLists.ComplexList;
var simpleList  = ColourLists.SimpleList;
var productName = "iPad Pro(9.7) 32GB in Rose Gold";
var productColour = string.Empty;

foreach(var colour in complexList)
{
    if(productName.Contains(colour, StringComparison.Ordinal))
    {
        productColour = colour;
        break;
    }
}

if(string.IsNullOrEmpty(productColour))
{
    foreach (var colour in simpleList)
    {
        if (productName.Contains(colour, StringComparison.Ordinal))
        {
            productColour = colour;
            break;
        }
    }
}

This would return your complex colour (in this scenario, Rose Gold would be found in the first foreach loop). If no complex colour was found, then it would return a simple colour (assuming there is one). You can set your productName variable to pull it directly from a model or object instead of being a string like how I've done it.

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