C# String Join(String, IEnumerable)

Description

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

Syntax

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


[ComVisibleAttribute(false)]//w  w w  .  j  a v  a  2  s.  c  om
public static string Join(
  string separator,
  IEnumerable<string> values
)

Parameters

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

  • 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 strings to concatenate.

Returns

String.Join(String, IEnumerable<String>) 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


using System;/*  w ww .  j  av a 2 s  .com*/
using System.Collections.Generic;
using System.Linq;

public class Example
{
   public static void Main()
   {
      string output = String.Join(" ", GetAlphabet().Where( letter => 
                      letter.CompareTo("M") >= 0));
      Console.WriteLine(output);  
   }

   private static List<string> GetAlphabet()
   {
      List<string> alphabet = new List<string>();
      
      alphabet.Add("A");
      alphabet.Add("X");
      alphabet.Add("Z");
      return alphabet; 
   }
}

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