Nifty .NET Part #1: String.Join

In the first part of the nifty .NET series I will blog about the method String.Join. First let see how the method definition looks like:

public static string Join(string separator, params string[] value)

Like the name suggests this method can join a bunch of strings together with a given separator.

Below an example of the method:

   1:  using System;
   2:   
   3:  public class MyClass
   4:  {    
   5:      public static void Main()
   6:      {
   7:          string[] array = new String[] { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" };
   8:          
   9:          string phrase = String.Join(" ", array);
  10:          
  11:          Console.WriteLine(phrase);
  12:          
  13:          Console.ReadLine();
  14:      }
  15:  }

You probably already guessed it, that the output of this console application will be “The quick brown fox jumps over the lazy dog”  because I used a space as separator.

With the overload below you can Join a specific part of the array together:

public static string Join(string separator, string[] value, int startIndex, int count)

There are also some more overload and generic variations on the String.Join listed below:

And for the completeness the msdn pages of the methods discussed above:

From now on you will find some cases where its very nifty to use the String.Join method.

2 Comments

Comments have been disabled for this content.