Search code examples
c#dictionarylevenshtein-distancefuzzy-search

How to search as a dictionary in C#


I'm try to find the search term in my term collection.

array of term collection :

[0] "windows"
[1] "dual sim"
[2] "32 gb"
[3] "Intel i5"

Now I search bellow term

search term= "32 gb"                 return -> 2 (position of array)
search term ="android 32 gb"         return -> 2 (position of array)
search term ="android mobile 32 gb"  return -> 2 (position of array)
search term= "32 GB"                 return -> 2 (position of array)
search term= "32gb"                  return -> not match 
search term= "dual sim 32"           return -> 1 (position of array)

So how can do like this in C# .net Can any search library or search dictionary provide this feature

Please advise/suggestion for same

Thanks!


Solution

  • You could use Array.FindIndex and do this.

    var array = new string [] {"windows","dual sim","32 gb","Intel i5"};
    
    string searchString = "android 32 gb";
    var index = Array.FindIndex(array, x=> searchString.IndexOf(x) >=0);
    

    if you are looking for case insensitive search, use this.

    var index = Array.FindIndex(array, x=> searchString.IndexOf(x, StringComparison.CurrentCultureIgnoreCase) >=0);
    

    Check this Demo