Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of opening the file every time, if the file is not huge and it is not updated while your program is running, perhaps you can read it just once and create a lookup table that can be searched, like in the example below. If you need case insensitive comparisons, or a more complicated way to do partial color matching, you will need to add that.</p> <pre><code>public class ColorLookUpTable { private readonly IDictionary&lt;string, string&gt; _colorTable; private ColorLookUpTable(IDictionary&lt;string, string&gt; colorTable) { _colorTable = colorTable; } public static ColorLookUpTable LoadFromFile(string fileName) { var colorTable = new Dictionary&lt;string, string&gt;(); using (var reader = File.OpenText(fileName)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var colorPairs = line .Split(new [] { ':' }, StringSplitOptions.RemoveEmptyEntries) .Where(f =&gt; !string.IsNullOrWhiteSpace(f)) .Select(f =&gt; f.Trim()); foreach (var colorPair in colorPairs) { var fields = colorPair.Split(new [] { '#' }, StringSplitOptions.RemoveEmptyEntries).ToArray(); if (fields.Length == 2) colorTable[fields[1]] = colorTable[fields[0]]; } } } return new ColorLookUpTable(colorTable); } public string FindColorCodeExactMatch(string colorName) { string colorCode = null; if (_colorTable.TryGetValue(colorName, out colorCode)) return colorCode; return null; } public string FindColorCodePartialMatch(string colorName) { var colorCode = FindColorCodeExactMatch(colorName); if (colorCode == null) // No exact match. Need to do work // very simple partial mathching. colorCode = _colorTable.Keys.Where(k =&gt; k.Contains(colorName)).FirstOrDefault(); return colorCode; } } </code></pre> <p>Usage:</p> <pre><code>var myColorLookupTable = ColorLookUpTable.LoadFromFile("colorCode.txt"); var colorCode = myColorLookupTable.FindColorCodeExactMatch("White"); var another = myColorLookupTable.FindColorCodePartialMatch("ack"); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload