-1

I have been trying to find resources online regarding inserting a string containing commas into a csv without separating that string into separate columns.

I have it working with other characters, just not commas since it's a string in C#.

My current solution is to replace the commas with pipes for that last column C.

row.AppendLine(A, B, C"); 
foreach (var x in [datasource]()) {
  row.AppendLine(x.A + "," + x.B + "," + x.C.Replace(",", "|")); 
}
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Does this answer your question? [Dealing with commas in a CSV file](https://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file) – Christopher May 24 '20 at 21:47
  • Thanks, I have been working with that article and a similar, good start, just needs polish to finish. –  May 24 '20 at 21:50
  • 3
    You really should not be writing CSV creation or parsing code yourself. It is too easy to make something wrong. And there are *innumerable* options out there already. – Christopher May 24 '20 at 21:50
  • Thanks I agree, however, it's just a quick parse for a UI display. that's why I still have to use commas instead of |'s for example. –  May 24 '20 at 21:52
  • Rephrase to include a specific question that you would like the community to answer. – Jesse Johnson May 24 '20 at 22:39
  • 1
    It's not really clear what you're asking here. If you're expecting commas to be part of the data in the strings, you need to use some kind of delimiter to indicate where the data begins/ends, typically double quotes. – Evan Trimboli May 24 '20 at 23:21
  • The string in question need to be enclosed in double quote marks in the CSV file. (Or another symbol of your choice, but double-quotes are pretty standard - most CSV parsing software would recognise that by default). – ADyson May 24 '20 at 23:26
  • @ADyson, can you show the corrected code please? –  May 26 '20 at 11:15
  • @EvanTrimboli Please explain what is not clear? It's a string, it contains commas. Now...csv file. However, syntax errors when trying to insert quotes and "". Can you show the syntax using my code? –  May 26 '20 at 11:17
  • @ChristopherMarcomCentral there are plenty of suggestions online already for how to do it - e.g. see https://stackoverflow.com/questions/7475536/parsing-a-csv-with-comma-in-data . That's just one source. Here's another: https://stackoverflow.com/questions/51650343/csv-file-with-cells-containing-commas-reading-data-into-c-sharp-program . you can find more quite easily. I don't think there's much point in me repeating all that or adding more code, unless/until you've tried some solutions and found a problem with them. – ADyson May 26 '20 at 11:45
  • Have a look here: https://stackoverflow.com/questions/5745986/auto-quotes-around-string-in-c-sharp-build-in-method – Evan Trimboli May 26 '20 at 12:08
  • @ADyson, and – Evan Trimboli thank you both, I will try these now and let know the results. –  May 26 '20 at 21:05

1 Answers1

1

Thank you both @Evan and @ADyson for finding out how to communicate the problem. This is the only solution that works, otherwise, str.replace the , with another character.

Auto quotes around string in c# - build in method?