Advertisement



< Prev
Next >



C# String Join() method




In C#, the Join() method of String class is used to join/concatenate the elements of an array or the elements of a collection by using a specific separator between each element. This separator is specified within the call to the Join() method.




Some overloads of Join() method


All the overloads of the Join() method are static in nature.

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.






Example of Join(String, String[ ]) method


In the upcoming code, we are calling one of the overloaded versions of the Join() method, which concatenates the elements of a String array by using a separator of type String between each element.

//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);
}
}


Output is -


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

As you can see in the self-explanatory example, we have joined the elements of a String array by calling the Join() method with a String separator value i.e. a blank space " ".


Advertisement




Example of Join(String, Object[ ]) method


In the upcoming code, we are calling one of the overloaded versions of the Join() method, which concatenates the elements of an Object array by using a separator of type String between each element.

//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);
}
}


Output is -


Elements of an Object array to Join by | separator:
A
A
A
A
Result of calling the Join() method: A|A|A|A

As you can see in the self-explanatory example, we have joined the elements of an Object array by specifying a String separator value of a vertical bar "|".




Example of Join(String, IEnumerable<String>) method


In the upcoming code, we are calling one of an overloaded versions of the Join() method, which concatenates the members of a constructed IEnumerable<String> collection of type String by using a String separator between each member.

//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);
}
}


Output is -


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

As you can see in the self-explanatory example, we have joined the elements of an IEnumerable<String> collection of type String by specifying a String separator value of a colon ":".




Example of Join<T>(String, IEnumerable<T>) method


In the upcoming code, we are calling one of the overloaded versions of the Join() method, which concatenates the members of a constructed IEnumerable<T> collection of any type by using a String separator between each member.

//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);
}
}


Output is -


Elements of an Object array to Join by - separator:
71
12
3
5
Result of calling the Join() method: 71-12-3-5

As you can see in the self-explanatory example, we have joined the elements of an IEnumerable<T> collection of type int by specifying a String separator value of a space "-".




Please share this article -





< Prev
<Next >
< C# Contains() Method
C# Exception Handling >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement