Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how add the list of strings into string using linq?

Tags:

string

c#

list

linq

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.

like image 750
ratty Avatar asked Jan 27 '11 09:01

ratty


3 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.)

like image 156
LukeH Avatar answered Oct 03 '22 17:10

LukeH


Why not just string.Join? Using Linq would be an overkill.

like image 34
Vlad Avatar answered Oct 03 '22 19:10

Vlad


Quick & dirty:

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

string output = s + string.Join("", list.ToArray());
like image 27
saku Avatar answered Oct 03 '22 19:10

saku