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
Assuming your string format is as follows
I’ll take
"N, Very Light Brown"
as an example. You can split these kind of strings this way:Based on the information provided and under the assumption that the row will be in the following format:
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:
It can be called in the following manner:
It seems to me that this works fine: