c# - How to append a substring to a string

Append a substring to a String
The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects.

The following .net c# tutorial code demonstrates how we can append a substring to a String. In this .net c# tutorial code we will append a substring to a String object. Here we will append a substring ‘ Laceflower.’ to the String instance using the String Insert() method and we also add a substring to the String instance using a plus operator.

We can simply append a substring to a String object by a plus operator. The plus operator adds the specified substring at the end of the specified String instance. The result also returns a String object.

The String Insert() method returns a new String in which a specified String is inserted at a specified index position in the instance. So using the String Insert(int startIndex, string value) method we can append a substring to the String instance.

We have to pass the length of the String instance for the startIndex parameter and the substring for the value parameter. So, the method returns a String object where the specified substring is appended to the source String. The Insert() method throws ArgumentNullException if the value is null. It also throws ArgumentOutOfRangeException if the startIndex is negative or greater than the length of this instance.

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

<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
        //this section create a string variable.
        string stringPlants = "Inkberry. Juneberry. Kousa. Kudzu.";
        Label1.Text = "string of plants..................<br />";
        Label1.Text += stringPlants;

        //this line append plant 'Laceflower' in string (insert at string last)
        stringPlants = stringPlants.Insert(stringPlants.Length, " Laceflower.");
        //another way to append string
        //stringPlants += " Laceflower.";

        Label1.Text += "<br /><br />string after append ' Laceflower.'...........<br />";
        Label1.Text += stringPlants;
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string append</title>  
    <form id="form1" runat="server">  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string append
        <hr width="550" align="left" color="Gainsboro" />  
        <br /><br />
            Text="string append"  
More c# examples