How to add a list of strings to a string using linq?

My List consists of {"a","b","c"}I have a string s contains {"alphabets"}.i how to add a list to a string. I need the final output in s, like this `{" alphabetsabc "}. I like to do this with linq.

+5
source share
4 answers

Using LINQ or even Joinin this case will be redundant. Concatwill do the trick perfectly:

string s = "alphabets";
var list = new List<string> { "a", "b", "c" };

string result = s + string.Concat(list);

(Note: if you are not using .NET4, you need to use it instead string.Concat(list.ToArray()). An overload Concatthat accepts IEnumerable<T>does not exist in earlier versions.)

+7
source

string.Join? Linq .

+5

:

List<string> list = new List<string>() {"a", "b", "c"};
string s = "alphabets";

string output = s + string.Join("", list.ToArray());
+2

Aggregate, LINQ.

+1

All Articles