This is my list:
List<string> elements = new List<string> { "apple", "orange", "peach" };
I need a method with this return value:
string result = "'apple', 'orange', 'peach'";
As you see the result add "'"
to the first of each string, also at the end of them, then all of them joined with ", "
. So what is your suggestion to do it fast and fluent? also consider performance issues, and maybe this list have been a lot of elements, how about that?
Throwing my suggestion in:
string result = string.Join(", ", elements.Select(e => "'" + e + "'"));
How about
string result = string.Empty;
if (elements.Count > 0)
result = "'" + string.Join("', '", elements) + "'"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With