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.

SYNTAX


Substring(start index,length of String);

                                DEMO


HTML CODING


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table><tr><td></td><td>
        <asp:Label ID="lblString" runat="server" Font-Size="Larger" ForeColor="#CC3300" Text="AspDotNet"></asp:Label>
        </td></tr>
        <tr><td></td><td></td></tr>
        <tr><td></td><td>
            <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" />
            </td></tr>
        <tr><td></td><td>&nbsp;</td></tr>
        <tr><td></td><td>
            <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>
            </td></tr>
    </table>
    </div>
    </form>
</body>
</html>

C# CODING


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








0 comments:

Post a Comment