c# - How to join an array elements into a new string

String Join() Method
The following asp.net c# example code demonstrate us how can we use .net framework's String Class Join() method in an asp.net application. String.Join() method allow us to concatenate the elements of a specified array or the members of a collection, using the specified separator between each element or member.

This member is overloaded, those are String.Join(String, IEnumerable<String>), Join<T>(String, IEnumerable<T>), Join(String, Object[]), Join(String, String[]) and Join(String, String[], Int32, Int32). In this tutorial, we only discuss about String.Join(String, String[]) overloaded method.

String.Join(String, String[]) method concatenate all the elements of a string array, using the specified separator between each element. This method require to pass two parameters named 'separator' and 'value'. The 'separator' parameter value represent a string to use as a separator and 'value' parameter represent an array that contains the elements to concatenate.

In this example, we concatenate a string array's elements by using a comma and a white space (, ) separator. Finally we get a System.String object from a specified array elements where array items are separated by specified separator.
Join.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    private string[] colorArray = { "Red", "Green", "Yellow", "HotPink", "SeaGreen" };
    
    protected void page_Load(object sender, System.EventArgs e) {
        if(!this.IsPostBack)
        {
            ListBox1.DataSource = colorArray;
            ListBox1.DataBind();
        }
    }
    protected void Button1_Click(object sender, System.EventArgs e) {
        string testString = string.Join(", ", colorArray);
        Label1.Text = "Joined successfully: " + testString;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to join an array of strings (elements) into a new string with specific separator</title>
</head> 
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green">asp.net string example: Join()</h2>
        <asp:Label 
             ID="Label1" 
             runat="server" 
             Font-Size="Large"
             ForeColor="Crimson"
             Font-Bold="true"
             Font-Italic="true"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2" 
             runat="server" 
             ForeColor="DodgerBlue"
             Font-Bold="true"
             Text="ListBox populated by an array"
             >
        </asp:Label>
        <br />
        <asp:ListBox 
             ID="ListBox1" 
             runat="server" 
             BackColor="DodgerBlue" 
             ForeColor="Snow"
             >
        </asp:ListBox>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             OnClick="Button1_Click"
             Font-Bold="true"
             Text="Join array elements and build String"
             ForeColor="DodgerBlue"
             />   
    </div>
    </form>
</body>
</html>
More c# examples