0
List<String> A = new List<string>();
A.Add("1");
A.Add("2");
A.Add("3");

string joined = String.Join("\",\"", A);

So geting output string like 1","2","3

But we requred joined= "1","2","3"

How can this possible

thanks

1
  • You simply joined items of list A using a separator which in your case is ",", the output would be like this: item1<separator>item2<separator>item3 (in your case separator is ","). if you want heading and trailing double quote, you should add it manually. Commented Sep 24, 2014 at 5:50

2 Answers 2

2
string joined = "\"" + String.Join("\",\"", A) + "\"";

or maybe

string joined = String.Join(",", A.Select(s => "\"" + s + "\""));
Sign up to request clarification or add additional context in comments.

4 Comments

The first of these looks cleaner to me, in that it will produce far fewer temporary strings.
@JonSkeet for perfomance / computer - yes, for human - second variant should be better because you read code as logic without guessing the trick... regualr deal.
Yes, that's true. Maybe this calls for a custom JoinSurrounded method :)
@JonSkeet I bet you can write such method with plain IL :D
0

The String.Join method joins multiple items in a collection using the delimiter you specify.

So when you run this line of code:

string joined = String.Join("\",\"", A);

You've specified that the delimiter that separates your items is a comma in quotes:

","

That string only appears between items, not before the first one or after the last one.

You'll have to add a quote before the first and after the last manually.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.