Advertisement
Methods | Description |
---|---|
Join(String, String[ ]) | This method concatenates the elements of a String array by using a separator of type String between each element. |
Join(String, Object[ ]) | This method concatenates the elements of an Object array by using a separator of type String between each element. |
Join(String, IEnumerable<String>) | This method concatenates the members of a constructed IEnumerable<String> collection of type String by using a separator between each member. |
Join<T>(String, IEnumerable<T>) | This method concatenates the members of a constructed IEnumerable<T> collection of any type by using a separator between each member. |
//C# Example of Join(String, String[])
using System;
class StringJoin1
{
public static void Main()
{
//Creating a String object to specify the separator
String separator = " ";
//A String array
String[] strArr = {"May", "all", "your", "dreams", "come", "true"};
//Printing the elements of a String array to Join:");
Console.WriteLine("Elements of a String array to Join:");
foreach(String str in strArr)
Console.WriteLine("'" + str + "'" + " " );
//Calling the Join(String, String[]) method
//To concatenate the elements of a String array by using the specified separator.
String result = String.Join(separator, strArr);
//Printing the result of calling the Join() method
Console.WriteLine("Result of calling the Join() method: " + result);
}
}
Elements of a String array to Join:
'May'
'all'
'your'
'dreams'
'come'
'true'
Result of calling the Join() method: May all your dreams come true
Advertisement
//C# Example of Join(String, Object[])
using System;
class A
{
public static void Main()
{
//Creating a String object to specify the separator
String separator = "|";
//Creating 4 objects of class A
A ob1 = new A();
A ob2 = new A();
A ob3 = new A();
A ob4 = new A();
//Creating an Object array
Object[] obArr = { ob1, ob2, ob3, ob4};
//Printing the elements of an Object array to Join
//Printing an object of any class prints the name of its class.
Console.WriteLine("Elements of an Object array to Join by " + separator + " separator:");
foreach(Object ob in obArr)
Console.WriteLine(ob);
//Calling the Join(String, Object[]) method
//To concatenate the elements of an Object array by using the specified separator.
String result = String.Join(separator, obArr);
//Printing the result of calling the Join() method
Console.WriteLine("Result of calling the Join() method: " + result);
}
}
Elements of an Object array to Join by | separator:
A
A
A
A
Result of calling the Join() method: A|A|A|A
//C# Example of Join(String, IEnumerable<String>) method
using System;
using System.Collections.Generic;
class A
{
public static void Main()
{
//Creating a String object to specify the separator
String separator = ":";
//Creating a List collection to hold int values
List collection1 = new List();
collection1.Add("Always");
collection1.Add("look");
collection1.Add("at");
collection1.Add("the");
collection1.Add("bright");
collection1.Add("side");
collection1.Add("of");
collection1.Add("life");
//Printing the elements of a IEnumerable Collection of type String array to Join
//Printing an elements of this collection
Console.WriteLine("Elements of an Object array to Join by " + separator + " separator:");
foreach(String str in collection1)
Console.WriteLine(str);
//Calling the Join(String, IEnumerable<String>]) method
//To concatenate the elements of an IEnumerable collection of type String by using the specified separator.
String result = String.Join(separator, collection1);
//Printing the result of calling the Join() method
Console.WriteLine("Result of calling the Join() method: " + result);
}
}
Elements of an Object array to Join by : separator:
Always
look
at
the
bright
side
of
life
Result of calling the Join() method: Always:look:at:the:bright:side:of:life
//C# Example of Join<T>(String, IEnumerable<T>) method
using System;
using System.Collections.Generic;
class A
{
public static void Main()
{
//Creating a String object to specify the separator
String separator = "-";
//Creating a List collection to hold int values
List<int> collection1 = new List<int>();
collection1.Add(71);
collection1.Add(12);
collection1.Add(3);
collection1.Add(5);
//Printing the elements of a IEnumerable Collection of type String array to Join
//Printing an elements of this collection
Console.WriteLine("Elements of an Object array to Join by " + separator + " separator:");
foreach(int i in collection1)
Console.WriteLine(i);
//Calling the Join<T>(String, IEnumerable<T>]) method
//To concatenate the elements of an IEnumerable collection of a type int by using the specified separator.
String result = String.Join(separator, collection1);
//Printing the result of calling the Join() method
Console.WriteLine("Result of calling the Join() method: " + result);
}
}
Elements of an Object array to Join by - separator:
71
12
3
5
Result of calling the Join() method: 71-12-3-5
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement