Export (0) Print
Expand All

String.Join Method (String, IEnumerable<String>)

Updated: January 2011

Concatenates the members of a constructed IEnumerable<T> collection of type String, using the specified separator between each member.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[ComVisibleAttribute(false)]
public static string Join(
	string separator,
	IEnumerable<string> values
)

Parameters

separator
Type: System.String
The string to use as a separator.
values
Type: System.Collections.Generic.IEnumerable<String>
A collection that contains the strings to concatenate.

Return Value

Type: System.String
A string that consists of the members of values delimited by the separator string. If values has no members, the method returns String.Empty.

ExceptionCondition
ArgumentNullException

values is null.

If separator is null, an empty string (String.Empty) is used instead. If any member of values is null, an empty string is used instead.

Join(String, IEnumerable<String>) is a convenience method that lets you concatenate each element in an IEnumerable(Of String) collection without first converting the elements to a string array. It is particularly useful with Language-Integrated Query (LINQ) query expressions. The following example passes a List(Of String) object that contains either the uppercase or lowercase letters of the alphabet to a lambda expression that selects letters that are equal to or greater than a particular letter (which, in the example, is "M"). The IEnumerable(Of String) collection returned by the Enumerable.Where method is passed to the Join(String, IEnumerable<String>) method to display the result as a single string.


using System;
using System.Collections.Generic;
using System.Linq;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string output = String.Join(" ", GetAlphabet(true).Where(letter =>
                      letter.CompareTo("M") >= 0));
      outputBlock.Text += output + "\n";
   }

   private static List<string> GetAlphabet(bool upper)
   {
      List<string> alphabet = new List<string>();
      int charValue = upper ? 65 : 97;
      for (int ctr = 0; ctr <= 25; ctr++)
         alphabet.Add(Convert.ToChar(charValue + ctr).ToString());
      return alphabet;
   }
}
// The example displays the following output:
//      M N O P Q R S T U V W X Y Z


The following example uses the Sieve of Eratosthenes algorithm to calculate the prime numbers that are less than or equal to 100. It assigns the result to a List<T> object of type String, which it then passes to the Join(String, IEnumerable<String>) method.


using System;
using System.Collections.Generic;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      int maxPrime = 101;
      List<int> primes = GetPrimes(maxPrime);
      outputBlock.Text += String.Format("Primes less than {0}:", maxPrime) + "\n";
      outputBlock.Text += String.Format("   {0}", String.Join(" ", primes)) + "\n";
   }

   private static List<int> GetPrimes(int maxPrime)
   {
      int[] values = new int[maxPrime + 1];
      // Use Sieve of Erathsthenes to determine prime numbers.
      for (int ctr = 2; ctr <= (int)Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))); ctr++)
      {

         if ((int)values.GetValue(ctr) == 1) continue;

         for (int multiplier = ctr; multiplier <= maxPrime / 2; multiplier++)
            if (ctr * multiplier <= maxPrime)
               values.SetValue(1, ctr * multiplier);
      }

      List<int> primes = new List<int>();
      for (int ctr = 2; ctr <= values.GetUpperBound(0); ctr++)
         if ((int)values.GetValue(ctr) == 0)
            primes.Add(ctr);
      return primes;
   }
}
// The example displays the following output:
//    Primes less than 100:
//       2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97


Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Date

History

Reason

January 2011

Noted that an empty string is substituted for any null member of the collection.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft