I have List consists of {"a","b","c"} i have string s contains{"alphabets"} .i like to add the list to string. i need final output in s like this `{"alphabetsabc"}. i like to do this using linq.
4 Answers
Using LINQ, or even Join, would be overkill in this case. Concat will do the trick nicely:
string s = "alphabets";
var list = new List<string> { "a", "b", "c" };
string result = s + string.Concat(list);
(Note that if you're not using .NET4 then you'll need to use string.Concat(list.ToArray()) instead. The overload of Concat that takes an IEnumerable<T> doesn't exist in earlier versions.)
Comments
Why not just string.Join? Using Linq would be an overkill.
1 Comment
LukeH
And
Join itself would be overkill when you can use Concat!Quick & dirty:
List<string> list = new List<string>() {"a", "b", "c"};
string s = "alphabets";
string output = s + string.Join("", list.ToArray());
3 Comments
spender
In .NET4 they finally added an overload to string.Join that takes an IEnumerable of string. As such .ToArray would be superfluous.
LukeH
Using
Join is overkill in this case. What's wrong with Concat? msdn.microsoft.com/en-us/library/system.string.concat.aspxsaku
Nothing wrong with Concat just Join was the first thing that came to my mind :)