dcsimg
Results 1 to 2 of 2

Thread: Comparing strings in textfiles

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2012
    Posts
    138

    Comparing strings in textfiles

    Hi,

    I have two text files, one which contains raw data and another which is cleaned up.

    I was wondering is it possible to compare the two text files and if the first string matches in both text files then to copy the second string from the raw text file into the clean text file at the end of the line?

    Previously when comparing files all I've done is compared them as a whole rather than strings

    Code:
     'Read all of your files
            Dim file1 As String = IO.File.ReadAllText("E:\File1.txt")
            Dim file2 As String = IO.File.ReadAllText("E:\File2.txt")
    
            'Compare if they're the same
            If file1 = file2 Then
                MessageBox.Show("same")
                'They're the same
            Else
                MessageBox.Show("different")
                'They're different
            End If
    Any help is appreciated, thanks

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,256

    Re: Comparing strings in textfiles

    Quote Originally Posted by VnewB View Post
    Hi,

    I have two text files, one which contains raw data and another which is cleaned up.

    I was wondering is it possible to compare the two text files and if the first string matches in both text files then to copy the second string from the raw text file into the clean text file at the end of the line?

    Previously when comparing files all I've done is compared them as a whole rather than strings

    Code:
     'Read all of your files
            Dim file1 As String = IO.File.ReadAllText("E:\File1.txt")
            Dim file2 As String = IO.File.ReadAllText("E:\File2.txt")
    
            'Compare if they're the same
            If file1 = file2 Then
                MessageBox.Show("same")
                'They're the same
            Else
                MessageBox.Show("different")
                'They're different
            End If
    Any help is appreciated, thanks
    Since the whole text file is read in as one big string I assume when you say you've compared them as whole rather than strings you mean you want to compare each line.

    There are two ways to do this, since you're already reading the entire file into a string you could use those strings and split them on the Environment.NewLine chars to get each down to an array meaning each element in the array is a separate line within the file, then you simply loop one of them (the most important one) and compare each of them (this works well with small files).
    The 2nd would be to re-do the file reading, you could use a System.IO.StreamReader for each of the files (instead of IO.File.ReadAllText) and in a loop call .ReadLine() to get a single line at a time from each file (this works well with large files) and compare each line read at a time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width