Tuesday, 6 August 2013

Count Number of Words in Paragraph/Sentence inside Textbox using ASP.Net C#

Count Number of Words in a Paragraph inside Text box using ASP.Net C#

I will now illustrate an Example to count the Number of words present inside a paragraph.. 

Design Part:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Count Words in String</title>
    <form id="form1" runat="server">
        <asp:TextBox ID="txtString" runat="server" BackColor="#E0E0E0" ForeColor="Black"
            Height="100px" TextMode="MultiLine" Width="600px"></asp:TextBox><br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Count Words" /><br />
        <br />
        <asp:Label ID="Label1" runat="server" Font-Bold="true"  ForeColor="red"></asp:Label>


public partial class CountWord : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)


    public static int CountWordInString(string strNew)
        int length = 0;
        strNew = strNew.Trim();
        char[] LineDelimiters = { ' ', '\n', '\r' };
        string[] arr = strNew.Split(LineDelimiters);
        length = arr.Length;
        return length;
    protected void Button1_Click(object sender, EventArgs e)
        string str = txtString.Text.Trim();
        Label1.Text = "The Number of Words in the above String is: "+Convert.ToString(CountWordInString(str));

1 comment:

  1. Hi, Topics are well organized. Please try to change the UI of the website.
    Other than this, everything fine.


Thank You for Your Comments. We will get back to you soon.

back to top