简体   繁体   English

如何在包含其他颜色一部分的字符串中查找颜色

[英]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. 即,某些字符串将包含玫瑰金,而其他字符串将仅包含金,有时该字符串将包含Black,而其他时候则包含Jet Black或Midnight Black。 and when I use the CONTAINS keyword it works randomly. 当我使用CONTAINS关键字时,它会随机运行。

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". 在第二个列表中,输入纯色的单词,例如“ Gold”,“ Green”或“ 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). 这将返回您的复杂颜色(在这种情况下,将在第一个foreach循环中找到玫瑰金)。 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. 您可以将productName变量设置为直接从模型或对象中提取它,而不是像我那样将其作为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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