1

我正在尝试创建一个搜索功能,这样当我搜索 White 时,它​​应该返回“#FFFFFF”。此外,它应该不区分大小写并支持部分名称输入。我拥有的文本文件包含以下格式的所有颜色编码信息:

"#FFFFFF"#White: "#FF0000"#Red:

我设法理解并到目前为止所做的如下;这基本上是映射到文本文件并指示它写入到文档末尾的行。我不明白的是如何实现上述要求。

我知道它不是很多,但我不明白搜索功能背后的概念。

 protected void search_Click(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader("colorCode.txt");
            string line = null;

           while((line =sr.ReadLine()) !=null)
            {

            }
4

2 回答 2

2

在此示例中,colorCode将匹配项存储为colorToSearch

    protected void search_Click(object sender, EventArgs e)
{
    StreamReader sr = new StreamReader("colorCode.txt");
    string line = null;

    char []colorSeparator =  { ':' } ;
    string []colors ;

    char []colorCodeSeparator =  { '#' } ;
    string []codeAndColor ;

    bool found ;
    string colorToSearch ;
    string colorCode ;

    colorToSearch  = "White" ;
    found = false ;

    while(!found && (line =sr.ReadLine()) !=null)
    {
        //separate the content of the file "#FFFFFF"#White: "#FF0000"#Red: using ":" as separator
        colors = line.Split(colorSeparator) ;

        foreach(string color in colors)
        {
            codeAndColor = colors.Split(colorCodeSeparator) ;
            //codeAndColor[0] now contains the colorCode "#FFFFFF"
            //codeAndColor[1] contains the name of the color "White"

            //check if the name of the color in the file contains the colorToSearch allowing for case insensitive
            if(codeAndColor[1].ToUpper().Contains(colorToSearch.ToUpper()))
            {
                colorCode = codeAndColor[0].Substring(1,7) ; //the 1 is to skip the initial double quote, 7 is the number of caracter to take
                found = true ;
                //stop the foreach loop
                break ;
            }
        }
    }
}

如果您要进行更多搜索,我建议您Dictionary<string,string>只从文件中加载一次颜色并在字典中执行搜索

于 2013-08-29T16:43:34.163 回答
1

如果文件不是很大并且在程序运行时没有更新,而不是每次都打开文件,也许您可​​以只读取一次并创建一个可以搜索的查找表,如下例所示。如果您需要不区分大小写的比较,或更复杂的方式来进行部分颜色匹配,则需要添加它。

public class ColorLookUpTable
{
    private readonly IDictionary<string, string> _colorTable;

    private ColorLookUpTable(IDictionary<string, string> colorTable)
    {
        _colorTable = colorTable;
    }

    public static ColorLookUpTable LoadFromFile(string fileName)
    {
        var colorTable = new Dictionary<string, string>();
        using (var reader = File.OpenText(fileName))
        {
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var colorPairs = line
                    .Split(new [] { ':' }, StringSplitOptions.RemoveEmptyEntries)
                    .Where(f => !string.IsNullOrWhiteSpace(f))
                    .Select(f => 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 => k.Contains(colorName)).FirstOrDefault();
        return colorCode;
    }
}

用法:

var myColorLookupTable = ColorLookUpTable.LoadFromFile("colorCode.txt");
var colorCode = myColorLookupTable.FindColorCodeExactMatch("White");
var another = myColorLookupTable.FindColorCodePartialMatch("ack");
于 2013-08-29T16:45:16.550 回答