Reputation: 289
So to output some attributes to a csv, I'm using stringbuilder
which uses the commas as a natural delimiter for commas. The problem is when I add a string with commas inside in the string, it's splitting it into separate columns for me. so for ex
temp.Append("ab,cd,ef")
is giving me 3 separate columns. How do I output it as a single column with the commas intact.
it's the result of a capture group
match = Regex.Match(amt, "^\"([\\d,]+),(\\d\\d\\d)\"$");
and then I did
amt = Convert.ToString(match.Groups[1].Value) + Convert.ToString(match.Groups[2].Value);
so it takes in something like 123,123,123,123 with an unknown number of commas and I want to output the exact same thing with the last commas replaced by a period. But it's still coming out as
123 123 123 123 123
in separate columns
Upvotes: 0
Views: 177
Reputation: 18474
You need to wrap the entry in a quote
temp.Append("\"ab,cd,ed\"");
UPDATED AFTER REDITED QUESTION
From what you are saying you wish to convert 123,445,567,890
to 123,445,567.890
and you want to the CSV output to work:
You still need to wrap your result in quotes so you need
amt = string.Format("\"{0}.{1}\"", match.Groups[1].Value, match.Groups[2].Value);
Alternatively you could just replace the last ','
amt = new Regex(",(?=\\d{3}\"$)").Replace(x,".");
or without the regex.
char[] y = amt.ToCharArray();
y[amt.LastIndexOf(",")] = '.';
amt = string(y);
Upvotes: 7
Reputation: 7123
Fields with embedded commas must be delimited with double-quote characters.
example: "Anytown, WW" (if it is one field) had to be delimited in double quotes because it had an embedded comma.
Keep a look here
Upvotes: 0