What's the simplest way to encoding List<String> into plain String and decode it back?

Add a reference and using to System.Web, and then:

public static string Encode(IEnumerable<string> strings)
{
    return string.Join("&", strings.Select(s => HttpUtility.UrlEncode(s)).ToArray());
}

public static IEnumerable<string> Decode(string list)
{
    return list.Split('&').Select(s => HttpUtility.UrlDecode(s));
}

Most languages have a pair of utility functions that do Url "percent" encoding, and this is ideal for reuse in this kind of situation.


You could use the .ToArray property on the List<> and then serialize the Array - that could then be dumped to disk or network, and reconstituted with a deserialization on the other end.

Not too much code, and you get to use the serialization techniques already tested and coded in the .net framework.


You might like to look at the way CSV files are formatted.

  • escape all instances of a deliminater, e.g. " in the string
  • wrap each item in the list in "item"
  • join using a simple seperator like ,

I don't believe there is a silver bullet solution to this problem.