0

我想仅使用用户输入的部分文本在文本框中实现自动完成功能。例如,如果用户输入“123 abc”,我希望自动完成只使用“abc”部分。

我尝试用数据(超过 500,000 个项目)填充哈希集。然后使用正则表达式@"^[\d]*\s*"从字符串的开头成功删除数字和空格,所以我只剩下“abc”(但我想将数字留在文本框中),然后value.StartsWith("abc")通过遍历每个值来填充列表哈希集。最后,我将所有过滤的项目添加到组合框项目中。

这种方法非常慢,并且给我最多 5 秒或更长时间的暂停,文本框自动完成方法更快更好地实现。我可以只使用部分字符串的文本框自动完成功能,还是可以建议其他一些同样快的实现?

4

1 回答 1

0

您是否尝试过/考虑过二进制搜索方法?它可能会更快...

您只需将它们全部添加到列表中,对其进行排序,然后在搜索中使用它,如下面的代码所示......

另一种方法是将其存储在索引数据库中,这将使用文本索引非常快。

例如...

如果未找到完全匹配,List.BinarySearch 将返回比请求大的下一个项目的索引的补码。

//So, you can do it like this (assuming you'll never get an exact match):

var key = (cardName + Config.EditionShortToLong(edition)).ToLower();
var list = CardBase.cardList;

var index = ~list.BinarySearch(key);
return index != list.Count && list[index].StartsWith(key);

祝你好运...

http://msdn.microsoft.com/en-us/library/w4e7fxsh.aspx

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>();

        dinosaurs.Add("Pachycephalosaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Mamenchisaurus");
        dinosaurs.Add("Deinonychus");

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nSort");
        dinosaurs.Sort();

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nBinarySearch and Insert \"Coelophysis\":");
        int index = dinosaurs.BinarySearch("Coelophysis");
        if (index < 0)
        {
            dinosaurs.Insert(~index, "Coelophysis");
        }

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nBinarySearch and Insert \"Tyrannosaurus\":");
        index = dinosaurs.BinarySearch("Tyrannosaurus");
        if (index < 0)
        {
            dinosaurs.Insert(~index, "Tyrannosaurus");
        }

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }
    }
}
于 2012-10-06T19:29:22.907 回答