Please disable your adblock and script blockers to view this page
 

How to convert list of string into comma separated string in Asp.Net C#, VB

Introduction: In this article I am going to explain how to get comma delimited string from List<string> in Asp.Net using both C# and VB language. 

Get comma separated string from List of strings in Asp.Net C#, VB

Description:  Here I have demonstrated two different methods to create a comma separated list from List<string>. In first method I have used StringBuilder and in second I have used string.join method to convert the list of string into comma delimited string. Here in this example I have used comma as a delimiter but we can use any delimiter to separate the list of string.
Implementation: Let’s create a sample page from demonstration purpose.

Asp.Net C# Code:

First Method: Using StringBuilder to concatenate list of strings separated by comma

protected void Page_Load(object sender, EventArgs e)
    {
        //Create a List of string and add items
        List<string> str = new List<string>();
        str.Add("Sonam");
        str.Add("Kabeer");
        str.Add("Simran");
        str.Add("Arjun");
        str.Add("Randeep");

        //Get Comma Separated String from List of strings
        string Result = GeDelimmetedString(str, ", ");
        Response.Write(Result);
};

 Private string GeDelimmetedString(List<string> strList, string delimiter)
    {
       System.Text.StringBuilder sb = new System.Text.StringBuilder();
        foreach (string str in strList)
        {
            if (sb.Length > 0)
            {
                sb.Append(delimiter);
            }
            sb.Append(str);
        }
        return sb.ToString();
    }

Second Method: Using String.Join method to concatenate list of strings separated by comma

protected void Page_Load(object sender, EventArgs e)
    {
//Create a List of string and add items
List<string> EmpNames = new List<string>() { "Sonam", "Kabeer", "Simran", "Arjun","Randeep" };       
//Get Comma Separated String from List of strings
var Result = String.Join(", ", EmpNames.ToArray());
Response.Write(Result);
}

Asp.Net VB Code:

First Method: Using StringBuilder to concatenate list of strings separated by comma

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Create a List of string and add items
        Dim str As New List(Of String)()
        str.Add("Sonam")
        str.Add("Kabeer")
        str.Add("Simran")
        str.Add("Arjun")
        str.Add("Randeep")

        'Get Comma Separated String from List of strings
        Dim Result As String = GeDelimmetedString(str, ", ")
        Response.Write(Result)
    End Sub

    Private Function GeDelimmetedString(strList As List(Of String), delimiter As String) As String
        Dim sb As New System.Text.StringBuilder()
        For Each str As String In strList
            If sb.Length > 0 Then
                sb.Append(delimiter)
            End If
            sb.Append(str)
        Next
        Return sb.ToString()
    End Function

Second Method: Using String.Join method to concatenate list of strings separated by comma

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Create a List of string and add items
        Dim EmpNames As New List(Of String)() From { _
            "Sonam", _
            "Kabeer", _
            "Simran", _
            "Arjun", _
            "Randeep" _
        }
        'Get Comma Separated String from List of strings
        Dim Result = [String].Join(", ", EmpNames.ToArray())
        Response.Write(Result)
    End Sub

Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon