Q:

What is a more efficient / tidier way of breaking this string down?

I'm trying to break down this string into 2 colours and wondered if there is a neater way of achieving the same result?
// Obtain colour values
string cssConfig = "primary-colour:Red, secondary-colour:Blue";
var parts = cssConfig.Split(',');
var colour1 = parts[0].Split(':')[1];
var colour2 = parts[1].Split(':')[1];

A:

You could use regex
string cssConfig = "primary-colour:Red, secondary-colour:Blue";
var reg = new Regex(@"([\w\-]+)\:([\w\-]+)");
foreach (Match match in reg.Matches(cssConfig))
{

}

Also you could do something with LINQ
    var cssConfigDict = cssConfig.Split(',')
                        .Select(x => x.Split(':'))
                        .ToDictionary(x => x.FirstOrDefault(), y => y.LastOrDefault());

There is probably a better way with LINQ!

A:

A Regex is definitely a possibility, though whether it's "neater" or more readable depends on personal preference and your ability to read regular expressions. If the actual use case of this is really just reading those two colors and not an arbitrary amount of colors then I'd probably just stay with the original solution. The intent is quite clear while keeping the code simple and avoids subtle errors if Regexes aren't your strong point.
LINQ is probably the most readable variant and would easily allow reading multiple key-value pairs while still using simple splitting mechanism to parse the data.
From my experience what you should definitely avoid for the sake of maintainability is writing a hugely complex, generic and seemingly "neat" solution for a problem this small and simple.

