Count number of words in text file in ASP.NET using VB.NET

In this article you learn that how can you Count the Number of words in a Text File.
  • 3442
Download Files:
 

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.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.