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];
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With