我正在编写一个程序来从列表中搜索名称,即使关键字不在名称前面,我也需要找到它们(这就是我的意思是非前缀)
例如,如果我的列表是乐器,我在搜索文本框中输入“guit” 。
它应该找到名称“Guitar, Guitarrón, Acoustic Guitar, Bass Guitar, ...”
或类似这个Longdo Dictionary 的搜索建议。
这是我的简单而愚蠢的算法(这就是我能做的)
const int SEARCHROWLIMIT = 30;
private string[] DoSearch(string Input, string[] ListToSearch)
{
List<string> FoundNames = new List<string>();
int max = 0;
bool over = false;
for (int k = 0; !over; k++)
{
foreach (string item in ListToSearch)
{
max = (max > item.Length) ? max : item.Length;
if (k > item.Length) continue;
if (k >= max) { over = true; break; }
if (!Input.Equals("Search")
&& item.Substring(k, item.Length - k).StartsWith(Input, StringComparison.OrdinalIgnoreCase))
{
bool exist = false;
int i = 0;
while (!exist && i < FoundNames.Count)
{
if (item.Equals(FoundNames[i]))
{
exist = true;
break;
}
i++;
}
if (!exist && FoundNames.Count < SEARCHROWLIMIT)
FoundNames.Add(item);
else if (FoundNames.Count >= SEARCHROWLIMIT) over = true;
}
}
}
return FoundNames.ToArray();
}
我认为这种算法对于大量名称来说太慢了,经过几次反复试验,我决定添加 SEARCHROWLIMIT 来中断操作而且我还认为有一些现成的方法可以做到这一点。
另一个问题是我需要按弦乐、打击乐器等类别以及原产国搜索乐器。所以我需要按类型和国家过滤器搜索它们。
我怎样才能做到这一点?