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> </head> <body> <form id="form1" runat="server"> <div> <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> </div> </form> </body> </html>
Code-Behind:
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));
}
}
Hi, Topics are well organized. Please try to change the UI of the website.
ReplyDeleteOther than this, everything fine.
Thanks