mskuratowski
mskuratowski

Reputation: 4124

Join multiple strings with delimiters

I would like to join multiple strings in my format:

Like: string1:string2,string1:string2, and more...

I have list with string1 and string2 values.

string test = String.Join(",", mylist.Select(x => x.string1));

How can I join these variables in my format ?

Upvotes: 1

Views: 779

Answers (2)

Sébastien Sevrin
Sébastien Sevrin

Reputation: 5405

Assuming:

  • string1 and string2 are properties from a custom object
  • mylist is a generic list of this custom object

You could just change your Select like this:

string.Join(",", mylist.Select(x => string.Format("{0}:{1}", x.string1, x.string2));

Upvotes: 5

David Watts
David Watts

Reputation: 2289

This should do it for you

string result = string.Join(",", myList.Select(x=> string.Join(":", x.string1, x.string2)));

Upvotes: 2

Related Questions