Easiest way to find a particular string from comma separated strings

c#, csv

Solution

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:

- Create variable called `csv` that contains the text

- Split the string into multiple strings using the `Split` function

- Use a LINQ query to only get text you want, i.e.: select the strings that start with "jus", in this case.

- Use the `Distinct` method to remove the duplicate entries from the list.

Problem

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".

Original source

Related problems