user801207
user801207

Reputation: 13

compare string line by line

Anybody here can help me with this

Input file

  Type Reference      
   WIN  00001  
   WIN  00001   
   WIN  00001  
   MAC  00001  
   MAC  00001  

Basically I need to compare if the first 3 character that are not equal

preferred output will be

Type Reference

   WIN  00001  
   WIN  00001   
   WIN  00001  

Code below

Dim fh As StreamReader
Dim os as string
fh = new StreamReader("haggis.txt")
Dim s As String = fh.ReadLine()
While not s Is Nothing
   os = s.Substring(0,3) 
   if os <> os then
      Console.WriteLine("Write here")
   else

   end if    

   s = fh.ReadLine
End While
fh.Close()
End Sub

Upvotes: 0

Views: 616

Answers (2)

Mabdullah
Mabdullah

Reputation: 1013

@Yet Another Geek, you're close, If you don't want to just close the program you could close the stream and then put an Exit Sub call in the else statement. This would take you back to whatever called the sub and continue processing.

Upvotes: 0

Yet Another Geek
Yet Another Geek

Reputation: 4289

Declare a previousOS variable, and compare it with that.

e.g.

Dim fh As StreamReader
Dim previousOS as string REM to see what previous os was
Dim os as string
fh = new StreamReader("haggis.txt")
Dim s As String = fh.ReadLine()
While not s Is Nothing
  previousOS = os 
  REM save the old os in this variable before assigning it to a new variable
  os = s.Substring(0,3) 
REM check if the new os is equal to the previousOS string (null check if it the first time read
if previousOS <> Nothing AndAlso previousOS <> os then
   Console.WriteLine("Write here")
else

end if    

s = fh.ReadLine
End While
fh.Close()
End Sub

Upvotes: 1

Related Questions