2

I have to fetch distinct values from a comma separated string.The input string can contain duplicate values.This is for auto-complete feature. For example:

I have a string: shop,dell,image,just,just do,file,just,do,shop.... My requirement is that when I pass 'jus', the output string should be: "just,just do".

ekad
  • 14,436
  • 26
  • 44
  • 46
nitinvertigo
  • 1,180
  • 4
  • 32
  • 56
  • I have converted the string into array using string.split(,) and then searched in the array. – nitinvertigo Feb 11 '14 at 07:01
  • Of the two solutions provided, which of them is the fastest? because the input string is going to be huge.. – nitinvertigo Feb 11 '14 at 07:09
  • There is no difference between using LINQ through keywords or through a method chain; both get compiled to the same IL (`http://stackoverflow.com/a/16487601/1937294`). It seems like the efficiency is debated (`http://stackoverflow.com/questions/1182922/what-is-the-efficiency-and-performance-of-linq-and-lambda-expression-in-net`), but your original question asks for the easiest way. – kdh Feb 11 '14 at 07:13
  • @Andrew thanks for the explanation.Performance is the most important factor for me as well as the code shouldn't be too complex.Your code satisfies my requirements. – nitinvertigo Feb 11 '14 at 07:19

2 Answers2

8
var csv = "shop,dell,image,just,just do,file,just,do,shop";
var arr = csv.Split(',');

var suggested = from word in arr
                where word.StartsWith("jus")
                select word;
suggested = suggested.Distinct();

To explain this code line by line:

  1. Create variable called csv that contains the text
  2. Split the string into multiple strings using the Split function
  3. Use a LINQ query to only get text you want, i.e.: select the strings that start with "jus", in this case.
  4. Use the Distinct method to remove the duplicate entries from the list.
kdh
  • 927
  • 1
  • 8
  • 18
4

You will want to split your string by commas and then search the resulting strings.

string csvList = "shop,dell,image,just,just do,file,just,do,shop";
string search = "jus"; // your search string goes here
var splitResults = csvList.Split(',').ToList();
// improvement: cache SplitResults once, and retrieve it from cache on every search
var searchResults = splitResults.Where(x => x.StartsWith(search)).Distinct();

You can change the last line to use Contains to search within words or use StartsWith(search, StringComparison.OrdinalIgnoreCase) to ignore case where searching.

In the case of a very large input, you should be caching the List<string> splitResults so that you have the search items ready to go. If you have high volume, you definitely do not want to be splitting the csvList every single time that it is searched.

Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174