逐行比较字符串

时间:2011-06-16 10:07:03

标签: vb.net compare streamreader streamwriter

这里的任何人都可以帮助我

输入文件

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

基本上我需要比较前三个不相等的字符

首选输出

类型参考

   WIN  00001  
   WIN  00001   
   WIN  00001  

以下代码

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

2 个答案:

答案 0 :(得分:1)

声明一个previousOS变量,并将其与之比较。

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

答案 1 :(得分:0)

@Yet另一个Geek,你很接近,如果你不想关闭程序,你可以关闭流,然后在else语句中放一个Exit Sub调用。这会将你带回到所谓的sub并继续处理。