C# String Join(String, IEnumerable)

Description

String Join(String, IEnumerable <t>)concatenates the members of a collection, using the specified separator between each member.

Syntax

String.Join<T>(String, IEnumerable<T>) has the following syntax.


[ComVisibleAttribute(false)]// w w  w  .j  a  v a2  s .com
public static string Join<T>(
  string separator,
  IEnumerable<T> values
)

Parameters

String.Join<T>(String, IEnumerable<T>) has the following parameters.

  • T - The type of the members of values.
  • separator - The string to use as a separator. separator is included in the returned string only if values has more than one element.
  • values - A collection that contains the objects to concatenate.

Returns

String.Join<T>(String, IEnumerable<T>) method returns A string that consists of the members of values delimited by the separator string. If values has no members, the method returns String.Empty.

Example

This method is particular useful with Language-Integrated Query (LINQ) query expressions.


using System;/*ww  w.  ja  v  a2  s  .co  m*/
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 Main()
   {
      List<Animal> animals = new List<Animal>();
      animals.Add(new Animal("Squirrel", "Rodent"));
      animals.Add(new Animal("Gray Wolf", "Carnivora"));
      string output = String.Join(" ", animals.Where( animal => (animal.Order == "Rodent")));
      Console.WriteLine(output);  
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version