对txt文件执行搜索功能
本文关键字:搜索 功能 执行 文件 txt | 更新日期: 2023-09-27 18:29:43
我正在尝试创建一个搜索函数,这样当我搜索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)
{
}
在本例中,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>
中的颜色,并在字典上执行搜索
如果文件不是很大,并且在程序运行时没有更新,那么您可以只读取一次,然后创建一个可以搜索的查找表,而不是每次都打开文件,就像下面的例子一样。如果你需要不区分大小写的比较,或者更复杂的部分颜色匹配方法,你需要添加它。
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");