5

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 4

8

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

Sign up to request clarification or add additional context in comments.

Comments

5

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

1 Comment

And Join itself would be overkill when you can use Concat!
2

Quick & dirty:

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

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

3 Comments

In .NET4 they finally added an overload to string.Join that takes an IEnumerable of string. As such .ToArray would be superfluous.
Using Join is overkill in this case. What's wrong with Concat? msdn.microsoft.com/en-us/library/system.string.concat.aspx
Nothing wrong with Concat just Join was the first thing that came to my mind :)
1

You need the Aggregate method, if you really want to use LINQ.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.