473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how many times a word is in a text file

i want to find how many time a specified word from a textbox , is in a file text.
this is my code but its not enough because i want to split all the file text to find how time is that word in the file.please help me!


Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2. Public Class Form1
  3. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  4.         Dim streamreader As System.IO.StreamReader
  5.         Dim nr_fjales As Integer
  6.         nr_fjales = 0
  7.         Dim i As Integer
  8.         streamreader = File.OpenText("C:\Users\user\Desktop\file.txt")
  9.         Dim cont As String
  10.         cont = streamreader.ReadToEnd.Split(" ").ToString
  11.         For i = 0 To streamreader.EndOfStream
  12.             If TextBox1.Text = cont Then
  13.                 nr_fjales = nr_fjales + 1
  14.             End If
  15.         Next
  16.  
  17.         Label2.Text = nr_fjales
  18.  
  19.         streamreader.Close()
  20.  
  21.     End Sub
  22. End Class
Mar 9 '13 #1
6 3486
Rabbit
12,516 Expert Mod 8TB
In your loop, it makes no sense to loop to streamreader.EndOfStream. You should loop through each element in your cont array.
Mar 10 '13 #2
i never worked with files and i dont know well the sintax, pls do you have any solution
Mar 10 '13 #3
Mikkeee
94 64KB
Regular expressions are the way to go. Single line of code will get you what you need. Find the number of matches using RegEx.Matches(exp,matchterm).Count.
Mar 10 '13 #4
thanks a lot ,, i understood it a bit .. but can u help me with the code how to link it with the file?
Mar 10 '13 #5
Mikkeee
94 64KB
RegEx is super powerful for stuff like this. Read a few tutorials and it will really get you out of a bind.
Expand|Select|Wrap|Line Numbers
  1.         Dim contents As String = System.IO.File.ReadAllText("C:\Users\user\Desktop\file.txt")
  2.         Dim count As Integer = Regex.Matches(contents, TextBox1.Text).Count
  3.         MessageBox.Show(count)
  4.  
Mar 11 '13 #6
thanks a lot ,, you are nr1 :)
Mar 11 '13 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Noam Dekers | last post by:
Hi all, I would like to find a word stored in a text file. Structure: I have one file named keyWords.txt that stores some key words I'm interested in finding. In addition I also have a file...
8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
7
by: nephish | last post by:
Hey there, i have a text file with a bunch of values scattered throughout it. i am needing to pull out a value that is in parenthesis right after a certain word, like the first time the word...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
1
by: mart2006 | last post by:
Hi, I'm currently reading a text file via PHP which, in itself, is very easy. However I want to specifically get one word from the text file and assign it as a variable and I'm struggling like...
4
by: bigbagy | last post by:
Notes The programs will be compiled and tested on the machine which runs the Linux operating system. V3.4 of the GNU C/C++ compiler (gcc ,g++) must be used. A significant amount coding is...
3
by: mse07 | last post by:
hi all i search about statement in text file by this code : LineFlag = LineFlag + 1 Line Input #1, strLine If LineFlag = 15 Then If InStr(1, strLine, " Transfer completed...
0
by: carmen413 via VBMonster.com | last post by:
Dear All, my text file like following, a1, b1, c1, d1, e1 a2, b2, c2, d2, e2 a3, b3, c3, d3, e3 a4, b4, c4, d4, e4 .................
0
by: alivip | last post by:
I write code to get most frequent words in the file I won't to implement bigram probability by modifying the code to do the following: How can I get every Token (word) and ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.