skip to Main Content

I have this kind of string

K, Faint Brown
N, Very Light Brown

Now I need to enter this values in CSV file, as like below.

In Col1 it will be: K
In Col2 it will be: Faint
In Col3 it will be: Brown.

I have done this using split method but issue is if I need to enter value for second row then it should enter Very Light together in Col2, but it is entering only Very

So for second row in CSV it should look like this,

In Col1 it will be: K
In Col2 it will be: Very Light
In Col3 it will be: Brown.

My code is below :

`string[] colorvalues = prop.GetValue(item).ToString().Split(','); // Here first of all I am splitting using comma.

sw.Write(colorvalues[0] + ","); // Then in first column I am entering first char.

sw.Write(colorvalues[1] + ","); // Then in second column I am entering second char, but issue is here if it is Very Light then it is taking only Very.. But I need to enter full Very Light.

sw.Write(prop.GetValue(item).ToString().Split().Last() + ","); // At last I am entering Colour name.`

3

Answers


  1. Assuming your string format is as follows

    A string without commas, Color Brightness in n words Color Name in 1 word

    I’ll take "N, Very Light Brown" as an example. You can split these kind of strings this way:

    //You can either use StringSplitOptions.TrimEntries,
    //use a string with a comma and a whitespace as separator (", ")
    //or use TrimStart() on the color definition part of your string
    //so that you don't get the string with a whitespace at the start
    var commaSplit = prop.GetValue(item).ToString().Split(", ");
    
    //`commaSplit` now consists of 2 strings: "N" and "Very Light Brown"
    
    //We write the first string as you did in your example
    sw.Write(commaSplit[0] + ",");
    
    //Now let's work on the second string
    var colorDefinitionSplit = commaSplit[1].Split();
    
    //colorDefinitionSplit now consists of 3 strings: "Very", "Light" and "Brown"
    //or it might be 1 string (just "Brown")
    //or n strings ("Very", "Very", "Very" ... "Very", "Light", "Brown")
    //so we need to enter everything but the last word.
    
    //first let's check if we even have anything except just "Brown"
    if (colorDefinitionSplit.Length > 1)
    {
        //if we do, we join everything except "Brown"
        //here you can use .SkipLast(1) instead of [0..^1] if you like it
        //(or not using C# 8.0 and later), result is the same
        var brightnessString = string.Join(' ', colorDefinitionSplit[0..^1]);
        sw.Write(brightnessString + ",");
    }
    
    //and all that's left to do is to write "Brown"
    //same here, you can use Last() instead of [^1]
    sw.Write(colorDefinitionSplit[^1] + ",");
    
    
    Login or Signup to reply.
  2. Based on the information provided and under the assumption that the row will be in the following format:

    code, N(tone) color
    
    eg: N, very very extremely dark brown
    

    The idea is to get the code first, then the color – what we are then left is to extract the tone.

    Putting it all together I have wrote this method which returns a Tuple:

    public (string code, string tone, string color) GetCodeAndToneAndColor(string row)
    {
        var splitRow = row.Split(",");
        string code = splitRow.First().Trim();
        string colorAndTone = splitRow[1];
        string color = colorAndTone.Split(" ").Last().Trim();
    
        int colorIndex = colorAndTone.IndexOf(color, StringComparison.Ordinal);
        string tone = colorAndTone.Substring(0, colorIndex).Trim();
    
        return (code, tone, color);
    }
    

    It can be called in the following manner:

    var (code, tone, color) = GetCodeAndToneAndColor(row);
    
    Login or Signup to reply.
  3. It seems to me that this works fine:

    var input = @"K, Faint Brown
    N, Very Light Brown";
    
    var csv =
        from line in input.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries)
        let parts = line.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
        select $"{parts[0]},{String.Join(" ", parts[1..^1])},{parts[^1]}";
    

    csv

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search