开发者

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 N开发者_如何学Pythonothing
   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


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


@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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜