Sam
Sam

Reputation: 943

formatting string in C#

How can I convert this list of strings to comma separated value enclosed within quotes without any escape characters?

{"apple", "berry", "cherry"} => well, ""apple", "berry", "cherry""

Upvotes: 0

Views: 100

Answers (3)

smartcaveman
smartcaveman

Reputation: 42276

If I understood you correctly,

"\"" + String.Join("\", \"", new string[]{"apple","berry","cherry"}) + "\"";

or, alternatively,

String.Format("\"{0}\"", String.Join("\", \"", new string[] {"apple","berry","cherry"}));

Read more on System.String.Join(...).

Upvotes: 1

Cole Tobin
Cole Tobin

Reputation: 9426

If you are using C#:

using System;
string[] arr = new string[] { "apple", "berry", "cherry" };
string sep = "\",\"";
string enclosure = "\"";
string result = enclosure + String.Join(sep, arr) + enclosure;

Upvotes: 0

Ilya Ivanov
Ilya Ivanov

Reputation: 23646

Hope this will do the job

var ar = new []{ "apple", "berry", "cherry" };
var separator = "\",\"";
var enclosingTag = "\"";
Console.WriteLine ( enclosingTag + String.Join(separator, ar) + enclosingTag );

Upvotes: 0

Related Questions