1

I have a collection of source strings that I wish to concatenate into one destination string.

The source collection looks as follows:

{ "a", "b", "c" }

I want the output string to be:

abc

But sometimes, I want a separator as well. So for the same input, now the output is to be:

a-b-c

And finally, the input sometimes needs to be enclosed in other characters, in this case [], causing the output to be:

[a]-[b]-[c]

An empty source collection should yield an empty string. How would I go about this?

5
  • This is meant as a canonical Q&A. Feel free to edit or add your own answers, or find a proper Q&A that explain this in a not too localized way. Commented Feb 1, 2016 at 20:00
  • Is this a correct use of the word "delimiter"? I think a delimiter is a separator, so "separator and delimiters" doesn't really make sense. Perhaps just say "separator and enclosing brackets"? Commented Feb 1, 2016 at 22:19
  • @Mark what is the more general term, then? "Enclosure"? I specifically don't want to refer to the actual enclosing characters by name. :) Feel free to edit the question. Commented Feb 1, 2016 at 22:22
  • I can't think of one, which is why I reluctantly suggested "enclosing brackets". Perhaps go for "enclosing brackets/quotes" and edit the Q&A to include an example with brackets and an example with quotes? That will simultaneously handle most people who are searching with specific characters in mind (since you're usually going to be enclosing stuff in quotes or brackets, if you're enclosing it at all), plus it will be clear to anyone who sees the title that you're covering the generic case rather than some very particular one. Commented Feb 1, 2016 at 22:26
  • @Mark am on mobile, can't really edit code now. Thanks for the suggestion and still, feel free to improve. Commented Feb 1, 2016 at 22:28

1 Answer 1

8

You can do this using the static String.Join() method.

Its basic usage is as such:

string[] sourceData = new[] { "a", "b", "c" };
string separator = "";
var result = string.Join(separator, sourceData);

When you supply an empty separator, the passed values will simply be concatenated to this: "abc".

To separate the source data with a certain string, provide the desired value as the first argument:

string[] sourceData = new[] { "a", "b", "c" };
string separator = "-";
var result = string.Join(separator, sourceData);

Now the string "-" will be inserted between every item in the source data: "a-b-c".

Finally to enclose or modify each item in the source collection, you can use projection using Linq's Select() method:

string[] sourceData = new[] { "a", "b", "c" };
string separator = "-";
result = String.Join(separator, sourceData.Select(s => "[" + s + "]"));

Instead of "[" + s + "]" you'd better use String.Format() to improve the readability and ease of modification : String.Format("[{0}]", s).

Either way, that also returns the desired result: "[a]-[b]-[c]".

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

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.