Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prefix search through list/dictionary using .NET StringDictionary?

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.

like image 888
Matthijs Wessels Avatar asked Feb 25 '26 21:02

Matthijs Wessels


1 Answers

StringDictionary is merely a hash table where the keys and values are strings. This existed before generics (when 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).

like image 161
jason Avatar answered Feb 28 '26 11:02

jason