Andrew
Andrew

Reputation: 10033

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];

Upvotes: 6

Views: 159

Answers (5)

SirViver
SirViver

Reputation: 2441

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.

Upvotes: 3

David
David

Reputation: 8650

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!

Upvotes: 7

Rune FS
Rune FS

Reputation: 21742

If you know that the order will always be the same. Which you'd need for your solution to work anyways, you could do:

// Obtain colour values
string cssConfig = "primary-colour:Red, secondary-colour:Blue";
var parts = cssConfig.Split(',',':');
var colour1 = parts[1];
var colour2 = parts[3];

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176906

Check this its similar to your requirement , to get the dictionary object form the string

Regex To Linq to Dictionary in C#

string input = "abc:1|bbbb:2|xyz:45|p:120";
string pattern = @"(?<Key>[^:]+)(?:\:)(?<Value>[^|]+)(?:\|?)";

Dictionary<string, string> KVPs
    = ( from Match m in Regex.Matches( input, pattern )
      select new
      {
          key = m.Groups["Key"].Value,
          value = m.Groups["Value"].Value
       }
       ).ToDictionary( p => p.key, p => p.value );

foreach ( KeyValuePair<string, string> kvp in KVPs )
    Console.WriteLine( "{0,6} : {1,3}", kvp.Key, kvp.Value );

/* Outputs:
 abc :   1
bbbb :   2
 xyz :  45
  p  : 120
 */

Upvotes: 1

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174329

You could use the LINQ methods:

var colors = cssConfig.Split(',').Select(x => x.Split(':').Skip(1).Take(1));

Having said that, I would stick with your method. It is clear code that everyone understands. The regex version and the LINQ version is more obscure...

Upvotes: 2

Related Questions