views:

90

answers:

3

I was wondering if .NET offers any standard functionality for doing a prefix search through a list or a dictionary object. I came across the StringDictionary, but couldn't figure out if it can do that for me.

And if it can do a prefix search, can it also do substring search or let me search using something like a regular expression?

Thanks in advance.

+3  A: 

I don't believe StringDictionary supports a prefix search, but if you use a SortedList<,> you can binary search through the range of keys until you find the first entry before and after your prefix.

Jon Skeet
+2  A: 

I think the StringDictionary is old school (pre-generics). You should probably use a Dictionary(Of String, String) instead because it implements IEnumerable (think LINQ). One extremely lame thing about StringDictionary is that it's case-insensitive.

Josh Stodola
+4  A: 

StringDictionary is merely a hash table where the keys and values are strings. This existed before generics (so that Dictionary<string, string> was not possible).

The data structure that you want here is a trie. There are implementations on CodeProject:

  1. Phone Directory Implementation Using TRIE
  2. A Reusable Prefix Tree using Generics in C# 2.0

Or, if you're that kind of guy, roll your own (see CLRS).

Jason
+1 Great answer! I couldn't remember the exact name of it -- trie!
Josh Stodola