I have the below code which works fine to split a string and save key value pairs into a dictionary. Is there a more elegant and efficient way to do the same? Thank you!
private void SplitString()
{
string testConfig = "application?test1=value1&test2=value2&test3=value3";
var dict = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(testConfig) && testConfig.Contains("?"))
{
string parameters = testConfig.Substring(testConfig.LastIndexOf("?") + 1);
string[] parameterArray = parameters.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
if (parameterArray.Length > 0)
{
foreach (var item in parameterArray)
{
string[] param = item.Split(new char[] { '=' });
if (param.Length == 2)
{
switch (param[0].ToUpper())
{
case "TEST1":
dict.Add("TEST1", param[1]);
break;
case "TEST2":
dict.Add("TEST2", param[1]);
break;
case "TEST3":
dict.Add("TEST3", param[1]);
break;
}
}
}
}
}
}