23

I want to represent the following string:

aaaa,23,"something inside double quotes", 99, 8, 7

I was thinking to do this using String.Format:

StringBuilder.AppendLine(string.Format("{0},{1},{2},{3},{4},{5}",
    item.one, item.two, item.three, item.four, item.five, item.six));    

I need to wrap third argument {2} with double quotes.

Otiel
  • 18,404
  • 16
  • 78
  • 126
user1765862
  • 13,635
  • 28
  • 115
  • 220
  • Related: [How to double-quote a string in C#](http://stackoverflow.com/questions/14292652/how-to-double-quote-a-string-in-c-sharp/14292665#14292665) – Tim M. Mar 25 '13 at 08:00

4 Answers4

35
string.Format("{0}, {1}, \"{2}\", {3}, {4}, {5}", ...);
acrilige
  • 2,436
  • 20
  • 30
11

You can do like this :

string.Format("{0},{1},\"{2}\",{3},{4},{5}"
   , item.one
   , item.two
   , item.three
   , item.four
   , item.five
   , item.six);

Here is a good link where you can read more about this : http://msdn.microsoft.com/en-us/library/267k4fw5.aspx

Jonas W
  • 3,200
  • 1
  • 31
  • 44
6

You should add \ before qoutes:

stringbuilder.AppendLine(string.Format("{0},{1},\"{2}\",{3},{4},{5}", item.one, item.two, item.three, item.four, item.five, item.six));
Nuffin
  • 3,882
  • 18
  • 34
evgenyl
  • 7,837
  • 2
  • 27
  • 32
1

you can put \ symbol to indicate escape sequence followed by a reserved characters (usually \n, \0, \t, \r, \", etc)

tonyo
  • 678
  • 1
  • 7
  • 22