Showing posts with label SubString. Show all posts
Showing posts with label SubString. Show all posts

Get Count Of A Substring In A String Using Asp.Net C#

Get Count Of A Substring In A String Using Asp.Net C#

Give Input String To Textbox Get Count Without Using Loop Condition in Asp.Net C#.

                         DEMO



                               Download

                   HTML Coding


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Get Count Of A Substring In A String Using Asp.Net C#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
   
        Input String
        <asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
        Count String
        <asp:TextBox ID="txtCount" runat="server"></asp:TextBox>
       
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Count" />
        <asp:Label ID="lblCount" runat="server" Font-Size="Large" ForeColor="#FF0066"></asp:Label>
        <br />
   
    </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;

using System.Text.RegularExpressions;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int c = Regex.Matches(txtInput.Text.ToUpper(),txtCount.Text.ToUpper()).Count;

        lblCount.Text = c.ToString();
    }

}


First - Add New form - Select Textbox From Toolbox




Next - Add - Namespaces - Input String Change Regex.Matches - Get Count






Remove Textbox Particular Character Using Asp.Net C#

Remove Textbox Particular Character

If Textbox Enter Some Character Want to Remove After Particular Character use Below Coding in Asp.Net C#.


                       Download Coding
                                       Download
         
                              DEMO
HTML CODING


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="Remove  @" />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Remove @+1" />
        <asp:Label ID="Label1" runat="server" ForeColor="#FF0066"></asp:Label>
    </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;
using System.Text;
using System.Text.RegularExpressions;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }   
    protected void Button1_Click1(object sender, EventArgs e)
    {
       string Remove = TextBox1.Text.Substring(0, TextBox1.Text.IndexOf("@")); // Remove before @
        Label1.Text = Remove.ToString();    
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string Remove = TextBox1.Text.Substring(0, TextBox1.Text.IndexOf("@") + 1); // Remove Upto @
        Label1.Text = Remove.ToString();
    }

}


First Create New Webform - Add Label,Textbox,Buttons From Toolbox 





Next - Code Behind - Add Substring Method Remove Indexof  Textbox






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