逐行比较字符串

发布于 2024-11-15 20:30:07 字数 631 浏览 3 评论 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

完美的未来在梦里 2024-11-22 20:30:07

声明一个 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

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
何以心动 2024-11-22 20:30:07

@Yet Another Geek,你已经很接近了,如果你不想只是关闭程序,你可以关闭流,然后在 else 语句中放置 Exit 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文