Working with Join method of String Class

vishalneeraj-24503
Posted by in C# category on for Intermediate level | Points: 250 | Views : 2356 red flag

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

Introduction

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)
Where,
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.

Objective

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;

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

Response.Write(concatenate_str);
}

Output Would be:-


Vishal#Kumar#Neeraj#Pune

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:-

1,14,32,47

3rd Example:-

Using List<string> Collection:-

List<string> names = new List<string>();
names.Add("Vishal");
names.Add("Rajesh");
names.Add("Nitin");
names.Add("Vinod");

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

Conclusion

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

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

http://www.dotnetfunda.com

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)