Search code examples
c#.net.net-2.0

Ensure splitting comma separated string with numbers in string


I am trying to split the following string

string v = "A: 1,5 ,B:2,44,C: 3,54,D: 5,11";

string[] val = v.Split(',');

for (int i = 0; i < val.Length; i++)
{
    val[i] = val[i].ToString().Trim();
}

The problem is that there are numbers with commas inside so the splitting is not correct. Is there a way to get for example in my array the correct values "A:1,5","B:2,44","C:3,54","D:5,11"? I am using C# 2.0


Solution

  • If your format must be a letter followed by a colon then a floating point number, with commas separating the entries:

    string patternDelim = @"([A-Z]):"; // split on 'X:'
    //                    @"([A-Z]+):" // split on 'ABC:'
    //                    @"([A-Z][A-Z0-9]*):" // split on 'A1B2C3:'
    string[] values = Regex.Split(input, patternDelim);
    

    Because we've used a capturing group as a delimiter, it will end up in our output. So, at this point we've carved up the input into something like this:

    A
     1,5 ,
    B
    2,44,
    C
     3,54,
    D
     5,11
    

    Now we just need to extract these into their actual pairs of data:

    Dictionary<string, string> pairs = new Dictionary<string, string>();
    for (int ii = 0; ii < values.Length; ++ii)
    {
        // empty values are skipped
        if (values[ii].Length == 0) continue;
    
        string value = values[ii + 1].Trim()
                                     .TrimEnd(','); // remove any trailing commas
    
        pairs.Add(values[ii], value.TrimEnd());
        ii++; // use two each time
    }
    

    This will result in a dictionary of key-value pairs like so:

    A = 1,5
    B = 2,44
    C = 3,54
    D = 5,11
    

    Which you can parse as you wish using the appropriate CultureInfo or NumberFormatInfo. Note, this will also work for all manner of decimal separators, and even if thousands separators are present:

    Input = A: 1.000,5 ,B:2,44,C: 3,54e+38,D: -,11
    
    A = 1.000,5
    B = 2,44
    C = 3,54e+38
    D = -,11