Adam Vandenberg ([info]piehead) wrote,
@ 2005-11-14 10:43:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Entry tags:c#, dotnet, programming

C#: Join an array of strings

The built-in method String.Join() let's you join an array of strings with a delimiter. Except that it only lets you join an array of strings. string[]. This is an example of inhumane API design, as it requires gross incantations to get an ArrayList of strings joined.

Here's my method to join together an ICollection of any type of object, which are rendered to strings via ToString().

public static string Concat(ICollection items, string delimiter)
{
  bool first = true;

  StringBuilder sb = new StringBuilder();
  foreach(object item in items)
  {
    if (item == null)
      continue;

    if (!first)
    {
      sb.Append(delimiter);
    }
    else
    {
      first = false;
    }
    sb.Append(item);
  }
  return sb.ToString();
}

Stick this static method wherever you keep your other string utility functions.




(6 comments) - (Post a new comment)

I did it this way in Java
[info]keith83
2005-11-20 07:54 pm UTC (link)
I did it this way in Java. If you stick the first element of the string on there first thing you don't need to keep track of 'first'.

> ...as it requires gross incantations to get an ArrayList of strings joined.

Does C# not have an equivalent to Java's toArray()?

(Reply to this) (Thread)

Re: I did it this way in Java
[info]piehead
2005-11-20 09:35 pm UTC (link)
>> If you stick the first element of the string on there first thing you don't need to keep track of 'first'.

True, but this code tosses null objects, so technically I'd have to walk through the list looking for the first non-null object first.

>> Does C# not have an equivalent to Java's toArray()?

Yes, but, the String.Concat(Object[]) method doesn't take a delimiter anyway.

(Reply to this) (Parent)

RE: C#: Join an array of strings
(Anonymous)
2007-06-12 01:30 pm UTC (link)
Check this out: http://lastattacker.blogspot.com/2007/06/pretty-good-c-string-concatenation.html

(Reply to this) (Thread)

Re: C#: Join an array of strings
[info]piehead
2007-06-12 01:59 pm UTC (link)
Kind of lame; my function will join any IEnumerable set of objects (or at least, in my updated code I use an IEnumerable instead of ICollection, since I don't need the count).

This example requires to to:
* Make a List.
* Fill it.
* Call ToArray() on it.

Since my use cases are generally "less than a bunch of N", the efficiency gain doesn't matter for me.

(Reply to this) (Parent)

It's faster this way...
(Anonymous)
2009-03-02 05:56 am UTC (link)
It's faster to always append the delimiter and then chop off the last one at the end:

public static string Intersperse<T> (IEnumerable<T> array, string delimiter)
{
	string result;
	StringBuilder sb = new StringBuilder ();
	foreach (T i in array)
	{
		if (i == null)
			continue;
		sb.Append (i);
		sb.Append (delimiter);
	}
	result = sb.ToString ();
	return result.Substring (0, result.Length - delimiter.Length);
}

(Reply to this) (Thread)

Re: It's faster this way...
[info]piehead
2009-03-17 10:00 pm UTC (link)
The code above fails if there are no items in `array`.

(Reply to this) (Parent)


(6 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…