0

I am trying to save some variables to a file. I am doing it like this

file.Write(organisationId + ";" + Name + ";" + organisationsNummer + ";" + statusCode + ";" + remark + ";" + created + ";" + updated + Environment.NewLine);

It will write a new line for each organization. The problem is that one of the organizations has an organisationsNummer value of "997744276\r" It deletes the \r part and moves the rest to a new line. How can I save the variables as it is "997744276\r". I tried to do this @organisationsNummer but that didn't do the trick.

  • 2
    Does this answer your question? [Can I convert a C# string value to an escaped string literal](https://stackoverflow.com/questions/323640/can-i-convert-a-c-sharp-string-value-to-an-escaped-string-literal) – sticky bit Apr 08 '21 at 11:09
  • 1
    Where did you get this data from? Does "997744276\r" actually mean the number, a backslash and the letter r, or is it a data error from previous imports? Because "\r" is a control character called "carriage return" (from the old days of typewriters) that does exactly what you describe. – nvoigt Apr 08 '21 at 11:09
  • I get it from a WebAPI - it could be a mistake on their part. But I have to deal with it anyway. – Daniel Dahl Apr 08 '21 at 11:19
  • Sounds like you should Trim organisationsNummer – Jesse de Wit Apr 08 '21 at 12:27

1 Answers1

1

Please do not try to write csv files by hand. You are doomed. This is the first thing that occurs, next thing you know is you have names that include a ;. And then the next quirk comes around. And the next and the next and the next.

Get a library that writes CSV files for you. CsvHelper seems nice, but you can pick another if you like. Just don't waste hours upon hours of fixing your own bug ridden csv file writer please.

nvoigt
  • 75,013
  • 26
  • 93
  • 142