NoviceMe
NoviceMe

Reputation: 3256

Adding string in front and at end of a list

I have list of names:

IEnumerable<Name> names;
names = n.GetNames(abc);

It gets list like: Ken, John, Sam,... I want it to show like this: 'Ken', 'John', 'Sam',...

I tried this: string s = string.Join("',", names); but it gives result like: Ken', John', Sam',...

Is there a way to add "'" in front of these names in single line of code?

Upvotes: 2

Views: 414

Answers (2)

Daniel A. White
Daniel A. White

Reputation: 190942

Try this.

string s = string.Join(",", names.Select(s => string.Format("'{0}'", s)).ToArray());

Upvotes: 3

Anderson Pimentel
Anderson Pimentel

Reputation: 5767

I think you were almost there:

string s = "'" + string.Join("','", names) + "'";

Upvotes: 3

Related Questions