• 0

[VB.NET] Count a specific word in a text document?


Question

5 answers to this question

Recommended Posts

  • 0

EDIT: nvm my first example with a linq expression, it was way too inefficient.

As wrack says, the best way is to use Regex.

Imports System.IO
Imports System.Text.RegularExpressions

Module Module1

	Sub Main()
		Dim text = File.ReadAllText("G:\USER\Documents\MyFile.log")
		Dim count = Regex.Matches(text, "a_word").Count
		Console.WriteLine(count)
		Console.ReadKey()
	End Sub

End Module

This code first stores the contents of MyFile.log as a string in "text". Then it counts the number of occurences of "a_word" in it.

Edited by Dr_Asik
  • 0

Actually, for even better efficiency, you should use a StreamReader instead of File.ReadAllText().

Using tr As TextReader = New StreamReader("G:\USER\Documents\MyFile.log")
	 count = Regex.Matches(tr.ReadToEnd(), "a_word").Count
End Using

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • No, you dumba** The whole conversation was about having backups......... And yes, you can have the cloud to access them between your devices, but -always- have backups. It's not that hard to understand is it?
    • Playing Sonic on a touchscreen tablet is as terrible as I imagined.
    • It's weird how some people don't know how to read the article... "If you remember the tests SpaceX was performing around 2012 with Grasshopper, well Honda is at about the same stage with its reusable rocket."
    • Microsoft Project 2021 Professional for Windows now just $9.97 by Steven Parker Today's highlighted deal comes from our Apps & Software section of the Neowin Deals store, where you can save $240 off Microsoft Project 2021 Professional for Windows. Be the boss of any project with Microsoft Project Professional 2021. This powerful yet easy-to-use app lets you stay on track, from small tasks to larger projects. Manage your projects more efficiently with the right timelines, budgets, and resources. You'll get automated scheduling tools as well as built-in reports to help you reduce inefficiencies and make better, informed decisions. Project management has never been this easy with Microsoft Project Pro. Lifetime license for Microsoft Project Professional 2021 One-time purchase installed on 1 Windows PC for use at home or work Instant Delivery & Download – access your software license keys and download links instantly Free customer service – only the best support! Why Microsoft Project Professional 2021? Pre-built templates help you get your project started on the right track Sync with Project Online and Project Server Submit timesheets to capture time spent on project and non-project work Run what-if scenarios to make the most of your task assignments Auto-populate start and end dates based on dependencies Visually represent complex schedules with built-in multiple timelines Supports Long-Term Servicing Channel (LTSC) and is compatible with Office LTSC and Office 2021 Good to know Length of access: lifetime Redemption deadline: redeem your code within 30 days of purchase Access options: desktop Max number of device(s): 1 Version: 2021 Pro Updates included Microsoft Project 2021 Professional for Windows normally costs $249.99, but it can be yours for just $14.97 for a limited time, that's a saving of $235. For terms, specifications, and license info please click the link below. Use MSO5 when checking out for additional $5 off. Coupon Expires June 29 Get Microsoft Project 2021 Professional for Windows for just $9.97, or learn more Although priced in U.S. dollars, this deal is available for digital purchase worldwide. We post these because we earn commission on each sale so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin Whitelist Neowin by not blocking our ads Create a free member account to see fewer ads Make a donation to support our day to day running costs Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: Neowin benefits from revenue of each sale made through our branded deals site powered by StackCommerce.
    • Weird how some people don't need to crash 7 rockets before they get it right.
  • Recent Achievements

    • Week One Done
      slackerzz earned a badge
      Week One Done
    • Week One Done
      vivetool earned a badge
      Week One Done
    • Reacting Well
      pnajbar earned a badge
      Reacting Well
    • Week One Done
      TBithoney earned a badge
      Week One Done
    • First Post
      xuxlix earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      678
    2. 2
      ATLien_0
      286
    3. 3
      Michael Scrip
      224
    4. 4
      +FloatingFatMan
      197
    5. 5
      Steven P.
      137
  • Tell a friend

    Love Neowin? Tell a friend!