Jenninha
Jenninha

Reputation: 1377

C# Replace all elements of List<string> with the same pattern with LINQ

I have a C# List with thousands of strings:

"2324343"
"6572332"
"45122" ...

I would like to replace all of them with brackets around them, so then they would look like

"(2324343)"
"(6572332)"
"(45122)" ...

I know how to write a for loop and do this but I would like to know if there is a way to do it with LINQ and LAMBDA expression preferably. I am open to other suggestions as well.

Many thanks!

Upvotes: 12

Views: 7743

Answers (1)

Giedrius
Giedrius

Reputation: 8540

var resultList = list.Select(x => string.Format("({0})", x)).ToList();

Upvotes: 29

Related Questions