0

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

输入文件

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

基本上我需要比较前 3 个字符是否不相等

首选输出将是

类型参考

   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
4

2 回答 2

1

声明一个previousOS 变量,并与它进行比较。

例如

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
于 2011-06-16T10:13:54.283 回答
0

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

于 2011-06-16T16:19:31.303 回答