How to Append or Concatenate Multiple Strings in Asp.Net C# VB.Net

Concatenation is the process of appending one string with another string. In multiple purposes we need to concatenate simple strings to large strings. In C# VB.Net we found a lot of way to concatenate strings. To concatenate string variables, we can use the + or += operators, or the String.Concat, StringBuilder.Append methods. The best and easy way to append or concatenate strings is the + operator. Where as the best and efficient way of appending strings is StringBuilder.Append method. So my suggestion is use + or += operator while doing small append operations but better to use StringBuilder.Append method while doing work with large strings.

C# code to append Strings:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string str = "";
            // Using + operator to append or concatenate multiple strings
            str = "Welcome to codedisplay.com" +
                " Welcome to codedisplay.com" +
                " Welcome to codedisplay.com";
            //Response.Write(str);

            // Using += operator to append or concatenate multiple strings
            str = "Welcome to codedisplay.com";
            str += " Welcome to codedisplay.com";
            str += " Welcome to codedisplay.com";
            //Response.Write(str);

            // Using String.Concat method to append or concatenate multiple strings
            Object[] oItems = new Object[] { "Welcome to codedisplay.com", " Welcome to codedisplay.com", " Welcome to codedisplay.com" };
            //Response.Write(string.Concat(oItems));

            // Using  StringBuilder.Append method to append or concatenate multiple strings
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = 0; i < 3; i++)
                sb.AppendLine("Welcome to codedisplay.com");
            Response.Write(sb.ToString());
        }
    }

VB.Net code to append Strings:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim str As String = ""
            ' Using + operator to append or concatenate multiple strings
            str = "Welcome to codedisplay.com" & " Welcome to codedisplay.com" & " Welcome to codedisplay.com"
            'Response.Write(str);

            ' Using += operator to append or concatenate multiple strings
            str = "Welcome to codedisplay.com"
            str += " Welcome to codedisplay.com"
            str += " Welcome to codedisplay.com"
            'Response.Write(str);

            ' Using String.Concat method to append or concatenate multiple strings
            Dim oItems As [Object]() = New [Object]() {"Welcome to codedisplay.com", " Welcome to codedisplay.com", " Welcome to codedisplay.com"}
            'Response.Write(string.Concat(oItems));

            ' Using  StringBuilder.Append method to append or concatenate multiple strings
            Dim sb As New System.Text.StringBuilder()
            For i As Integer = 0 To 2
                sb.AppendLine("Welcome to codedisplay.com")
            Next
            Response.Write(sb.ToString())
        End If

    End Sub

Hope now you can concatenate or append strings in the most efficient and easiest way.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*