Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

count how many times a string is in a text file

Status
Not open for further replies.

Matta25

Technical User
Jan 6, 2004
82
0
0
US
Does any one know how to count how many times a sting is in a text file using VB.net. If you have a sample of opening the file and count the string and then writing the number to a new text file, that would be great.
Thank you.
 
Not tested, but something like:

Code:
    Dim sr As New IO.StreamReader("[b]SourceFileName[/b]")
    Dim sw As New IO.StreamWriter("[b]TargetFileName[/b]")
    Dim rx As New System.Text.RegularExpressions.Regex("[b]PatternYouAreLookingFor[/b]")

    Dim s As String = sr.ReadToEnd
    sr.Close()
    sw.Write(rx.Matches(s).Count)
    sw.Close()

    sr = Nothing
    sw = Nothing
    rx = Nothing

should be close.


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top