Concept of SubString Method Examples Using in C#

Concept of SubString Method

SubString  is method of a String Class in c#.

Substring Extracts Already Existing Strings.

It is called by string name followed by dot operator.

It requires Start Index and a Length of String then it will return completely new string with the characters in that range.


Substring(start index,length of String);



<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Label ID="lblString" runat="server" Font-Size="Larger" ForeColor="#CC3300" Text="AspDotNet"></asp:Label>
            <asp:Button ID="Button1" runat="server" Font-Size="Medium" Height="29px" OnClick="Button1_Click" Text="Get" Width="46px" />
            &nbsp;<asp:Button ID="Button2" runat="server" Font-Size="Medium" OnClick="Button2_Click" Text="Get1" />

            <asp:Button ID="Button3" runat="server" Font-Size="Medium" OnClick="Button3_Click" Text="Get2" />
            <asp:Label ID="lblDisplay" runat="server" Font-Size="XX-Large" ForeColor="#FF66CC"></asp:Label>
            <br />
            <br />
            <asp:Label ID="lblDisplay1" runat="server" Font-Size="XX-Large"></asp:Label>
            <br />
            <br />
            <asp:Label ID="lblDisplay2" runat="server" Font-Size="XX-Large" ForeColor="#CC3300"></asp:Label>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SubString : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    protected void Button1_Click(object sender, EventArgs e)

        //  Substring(start index,length of String);

        lblDisplay.Text = lblString.Text.Substring(6);   // Length

    protected void Button2_Click(object sender, EventArgs e)
        //  Substring(start index,length of String);

        lblDisplay1.Text = lblString.Text.Substring(0,3);   // Index start 0 To Length 3

    protected void Button3_Click(object sender, EventArgs e)
        //  Substring(start index,length of String);

        lblDisplay2.Text = lblString.Text.Substring(3,3);   // Index start 3 To Length 3



First Add New Web Form  -  Add Label & Button 

From Toolbox

Next - Assign Index & Length of SubString

Next - Run[F5]  Get  SubString  Result


Post a Comment