Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string join append each element text

Tags:

c#

i have array with n-elements;

var arr = ["answer1", "answer2", "answer3"];

I need such result:

{"text":"answer1"},{"text":"answer2"},{"text":"answer3"}

Tried sothing like this:

string.Join(",", arr);

But how add {"text":" "} to each element?

like image 356
Erfercfsfr Erferf Avatar asked Jun 29 '26 07:06

Erfercfsfr Erferf


1 Answers

You can use LINQ to select from the array with the additional text:

var arrayWithText = arr.Select(a => "{\"text\":\"" + a + "\"}");
var result = string.Join(",", arrayWithText);
like image 155
Steve Lillis Avatar answered Jun 30 '26 22:06

Steve Lillis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!