Compartir a través de


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

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: January 2011

Concatenates the members of a string collection, using the specified separator between each member.

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

Syntax

'Declaration
<ComVisibleAttribute(False)> _
Public Shared Function Join(Of T) ( _
    separator As String, _
    values As IEnumerable(Of T) _
) As String
[ComVisibleAttribute(false)]
public static string Join<T>(
    string separator,
    IEnumerable<T> values
)

Type Parameters

  • T
    The type of the members of values.

Parameters

  • separator
    Type: System.String
    The string to use as a separator.

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.

Exceptions

Exception Condition
ArgumentNullException

values is nulla null reference (Nothing in Visual Basic).

Remarks

If separator is nulla null reference (Nothing in Visual Basic), an empty string (String.Empty) is used instead. If any member of values is nulla null reference (Nothing in Visual Basic), an empty string is used instead.

Join<T>(String, IEnumerable<T>) is a convenience method that lets you concatenate each member of an IEnumerable<T> collection without first converting them to strings. The string representation of each object in the IEnumerable<T> collection is derived by calling that object's ToString method.

This method is particular useful with Language-Integrated Query (LINQ) query expressions. For example, the following code defines a very simple Animal class that contains the name of an animal and the order to which it belongs. It then defines a List<T> object that contains a number of Animal objects. The Enumerable.Where extension method is called to extract the Animal objects whose Order property equals "Rodent". The result is passed to the Join<T>(String, IEnumerable<T>) method.

Imports System.Collections.Generic

Public Class Animal
   Public Kind As String
   Public Order As String

   Public Sub New(ByVal kind As String, ByVal order As String)
      Me.Kind = kind
      Me.Order = order
   End Sub

   Public Overrides Function ToString() As String
      Return Me.Kind
   End Function
End Class

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim animals As New List(Of Animal)
      animals.Add(New Animal("Squirrel", "Rodent"))
      animals.Add(New Animal("Gray Wolf", "Carnivora"))
      animals.Add(New Animal("Capybara", "Rodent"))
      Dim output As String = String.Join(" ", animals.Where(Function(animal) _
                                           animal.Order = "Rodent"))
      outputBlock.Text &= output & vbCrLf
   End Sub
End Module
' The example displays the following output:
'      Squirrel Capybara
using System;
using System.Collections.Generic;
using System.Linq;

public class Animal
{
   public string Kind;
   public string Order;

   public Animal(string kind, string order)
   {
      this.Kind = kind;
      this.Order = order;
   }

   public override string ToString()
   {
      return this.Kind;
   }
}

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      List<Animal> animals = new List<Animal>();
      animals.Add(new Animal("Squirrel", "Rodent"));
      animals.Add(new Animal("Gray Wolf", "Carnivora"));
      animals.Add(new Animal("Capybara", "Rodent"));
      string output = String.Join(" ", animals.Where(animal =>
                      (animal.Order == "Rodent")));
      outputBlock.Text += output + "\n";
   }
}
// The example displays the following output:
//      Squirrel Capybara

Examples

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 integer, which it then passes to the Join<T>(String, IEnumerable<T>) method.

Imports System.Collections.Generic

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim maxPrime As Integer = 100
      Dim primes As List(Of Integer) = GetPrimes(maxPrime)
      outputBlock.Text += String.Format("Primes less than {0}:", maxPrime) & vbCrLf
      outputBlock.Text += String.Format("   {0}", String.Join(" ", primes)) & vbCrLf
   End Sub

   Private Function GetPrimes(ByVal maxPrime As Integer) As List(Of Integer)
      Dim values(maxPrime) As Integer
      ' Use Sieve of Erathsthenes to determine prime numbers.
      For ctr As Integer = 2 To _
                           CInt(Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))))
         If CInt(values.GetValue(ctr)) = 1 Then Continue For

         For multiplier As Integer = ctr To maxPrime \ 2
            If ctr * multiplier <= maxPrime Then values.SetValue(1, ctr * multiplier)
         Next
      Next

      Dim primes As New System.Collections.Generic.List(Of Integer)
      For ctr As Integer = 2 To values.GetUpperBound(0)
         If CInt(values.GetValue(ctr)) = 0 Then primes.Add(ctr)
      Next
      Return primes
   End Function
End Module
' 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
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

Version Information

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

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

Change History

Date

History

Reason

January 2011

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

Customer feedback.