Skip to content

Instantly share code, notes, and snippets.

@cburgdorf
Created June 18, 2011 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cburgdorf/1033292 to your computer and use it in GitHub Desktop.
Save cburgdorf/1033292 to your computer and use it in GitHub Desktop.
Configuration parsing is fun
/// <summary>
/// Splits up an configuration string and returns an Dictionary<string, string>
/// (e.g. "CommandName='FilesOnly'; Directory='C:\\Test\\'" becomes
/// new Dictionary<string, string>
/// {
/// {"commandname", "filesonly"},
/// {"directory", "c:\\test\\"}
/// })
/// </summary>
private Dictionary<string, string> ParseString(string configurationString)
{
Func<string, string> sanitizeString = x => x.Trim().ToLower().Replace("'", "");
return configurationString
.Split(';')
.Select(x => x.Split('='))
.Where(x => x.Length == 2)
.ToDictionary(x => sanitizeString(x[0]), x => sanitizeString(x[1]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment