Question

I have a list of strings and I am concatenating it to flatten out the list by using the method seen here on DotNetPerls, http://www.dotnetperls.com/string-concat

My question is...in their List example where their output is "catdogperls" (see toward the bottom of the webpage, just before the Summary) how do I insert a # sign as a separator between "catdogperls" such that it becomes "cat#dog#perls"?

Était-ce utile?

La solution

In this case you don't want to use string.Concat(), you want to use string.Join(). This accepts a separator and an array of strings to join by that separator. For example:

var joined = string.Join("#", theArray);

This would place the string value in joined:

"cat#dog#perls"

(assuming, of course, that theArray contains those values)

Autres conseils

Try like this :

String.Join("#", catdogperls)

You're looking for String.Join(), which takes a collection and a separator.

Use string.Join(), it allows you to specify the separator you want between each string - it's been in the framework since v2.0.

In later versions of the framework it was extended so that you could pass in an IEnumerable instead of just an array.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top