Working with Join method of String Class

Posted by in C# category on for Intermediate level | Points: 250 | Views : 3366 red flag

Today we will learn about working with Join method of String Class.


As the name implies,Join means joining or concatenating string array to make into one.
It places the separator string between every element of the collection in the returned string.
Concatenates the specified elements of a string array, using the specified separator between each element.
A Join method is a STATIC method.We can directly access Join with String.

General Syntax of Join method:-
public static string Join(string separator,string[] value,int start_index,int count)
Separator :- It can be anything special character like Comma(,),Hash(#) or any character.
Value :- It specifies any string array.
Start_Index :- Array first index value.
Count :- Total number of index.


Working with Join method of String Class

Using the code

We can understand it by an example:-

1st Example:-
private void join_array()
string concatenate_str = string.Empty;

string[] str_array = { "Vishal", "Kumar", "Neeraj", "Pune" };
concatenate_str = string.Join("#", str_array);
catch (Exception ex)
throw ex;


Output Would be:-


2nd Example:-
string str = "1,4,14,32,47";
string concatenate_string = String.Join(",", str.Split(',').Where((x, index) => index != 1).ToArray());
Response.Write("<br/>" + concatenate_string);

Output Would be:-


3rd Example:-

Using List<string> Collection:-

List<string> names = new List<string>();

string name = string.Join("$", names.ToArray());
Response.Write("Name will be- " + name);

Output would be:-

Name will be- Vishal$Rajesh$Nitin$Vinod

4th Example:-

Using List<Class_Name> Collection:-

Suppose, i have a Class called Detail which contains Id and Name

/// <summary>
/// Summary description for Detail
/// </summary>
public class Detail
public Detail()

public int Id { get; set; }
public string Name { get; set; }

protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
List<Detail> lstDetails = new List<Detail>();
lstDetails.Add(new Detail() { Id = 1, Name = "Vishal" });
lstDetails.Add(new Detail() { Id = 2, Name = "Rajesh" });
lstDetails.Add(new Detail() { Id = 2, Name = "Nitin" });

string str_value = String.Join("*", lstDetails.Select(s => s.Name).ToArray());

Response.Write("Name will be- " + str_value);

Output would be:-

Name will be- Vishal*Rajesh*Nitin


Today,we have learned about working with Join method of String and its Usage.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: vishal kumar
Member Level: Platinum
Member Status: Member,MVP
Member Since: 11/5/2013 5:58:17 AM
Country: India

Login to vote for this post.

Comments or Responses

Posted by: Sheonarayan on: 12/19/2013 | Points: 25
Good one Vishal.

Login to post response

Comment using Facebook(Author doesn't get notification)