ARTICLE

How to Count Number of words in a Text File in ASP.NET using VB.NET

Posted by Shalini Juneja Articles | ASP.NET using VB.NET May 04, 2011
In this article you learn that how can you Count the Number of words in a Text File.
Download Files:
 
Reader Level:

Introduction

In this article we are discussing that how can we count the Number of words occurring in a text file and get that number  in the TextBox by some lines of code. The implementation of this example needs a TextBox. The way we have read the files. They will be in an array type structure. So every line will be stored in an Array. You can also achieve this by placing the contents of a file in to a String variable and then count them Number of new line characters.
 

Getting Started

  • Simply Create a new ASP.NET web application. 
  • Drag a TextBox on your page. The page will look like below.

    txtfile1.gif
     
  • You can also add control manually by the below code.

    <%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
        CodeBehind="Default.aspx.vb" Inherits="WebSite_Visitors._Default" %>
    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </
    asp:Content>
    <
    asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <br />
        No of Words are
       
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
    </
    asp:Content>
     
  • Then add the below code in code behind file.

       
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim M As Integer
            Dim j As Integer = -1
           
    Dim myReader As StreamReader = New StreamReader("d:\SP Change.txt")
           
    Dim line As String
            Do
                M += 1
                line = myReader.ReadLine()
           
    Loop Until line Is Nothing
            myReader.Close()
            myReader =
    Nothing
            TextBox1.Text = CStr(M)
       
    End Sub
     
  • Now run your application.


Output:

txtfile2.gif

Summary

In this article you learned that how to count Number of words in a text file.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor