Hello,
I am trying to extract some words from a Dictionary but only by giving the first letters.
So I have a Dictionary with several words.
I would like to list all the words of that Dictionary by giving at the Key only 2, or more first letters.
It seems if I understand correctly the theory that is not possible but maybe someone as an idea to get around the problem.
I agree with you but for the time being I am working with a SQL Database or with a tree but I have discovered the Dictionary which is very simple and very fast so that’s why I wanted to compare the different methods.
But if it is impossible to do do it with a Dictionary I will keep the previous methods.
I am not an expert but is it possible to create a class like Dictionary but with the possibility to find the values with the firth letters of the words?
Or you could just iterate over the dictionary checking the keys against your search parameters but at a guess the in-memory SQLite suggestion that Dave makes will probably be quicker.
Something along these lines should do the trick, but you may need to adjust it a bit depending on the structure of your dictionary.
[code]Public Function BeginsWith(extends str as String, txt as string) as Boolean
Dim Results as Boolean = False
if str.Lowercase.Left(txt.Len) = txt.Lowercase then
Results = True
end if
Return Results
End Function
Public Function ValuesBeginWith(extends dic as Dictionary, str as String) as Dictionary
dim Results as Dictionary
dim ct as integer = dic.Count
dim x as integer
for x = 0 to ct
dim k as String = dic.Key(x)
if dic.HasKey(k) then
dim v as string = dic.Value(k)
if v.BeginsWith(str) then
Results.Value(k) = v
end if
end if
next x
Return Results
End Function[/code]
An alternative to the second function would be one that simply returns a list of words that begin with what you want:
Public Function ValuesBeginWith(extends dic as Dictionary, str as String) as String()
dim Results() as String
dim ct as integer = dic.Count
dim x as integer
for x = 0 to ct
dim k as String = dic.Key(x)
if dic.HasKey(k) then
dim v as string = dic.Value(k)
if v.BeginsWith(str) then
Results.Append(v)
end if
end if
next x
Return Results
End Function
I believe this is essentially what you are asking for, if I am mistaken, please let me know.
And alternatively, if you are looking for just the keys, then the following should work:
Public Function KeysBeginWith(extends dic as Dictionary, str as String) as String()
dim Results() as String
dim ct as integer = dic.Count
dim x as integer
for x = 0 to ct
dim k as String = dic.Key(x)
if k.BeginsWith(str) then
Results.Append(k)
end if
next x
Return Results
End Function
Public Function KeysBeginWith(extends dic as Dictionary, str as String) as Dictionary
dim Results as Dictionary
dim ct as integer = dic.Count
dim x as integer
for x = 0 to ct
dim k as String = dic.Key(x)
if k.BeginsWith(str) then
Results.Value(x) = k
end if
next x
Return Results
End Function