问题描述
我正在使用vb.net Windows窗体应用程序.我想从行列表中删除行.
I'm using vb.net windows form app. I want to remove line from list of lines.
因此,如果该列表中存在文本框中的行,则要从列表中删除该行,并保存文件.
So if the line in textbox exists on that list, to be remove from list, and file to be saved.
我有一个包含数字列表的list.txt文件:
I have a file list.txt with list of numbers :
123-123
321-231
312-132
如果我在文本框内写:321-231,并且如果list.txt包含该行,则将其删除.所以结果必须是:
If I write in textbox : 321-231 and if list.txt contains that line then remove it.so result need to be :
123-123
321-132
我正在尝试使用此代码:
I was trying with this code :
Dim lines() As String
Dim outputlines As New List(Of String)
Dim searchString As String = Textbox1.Text
lines = IO.File.ReadAllLines("D:\list.txt")
For Each line As String In lines
If line.Contains(searchString) = True Then
line = "" 'Remove that line and save text file (here is my problem I think )
Exit For
End If
Next
推荐答案
在处理字符串时将其放入outputlines
列表中,除非它与键入的值匹配,如下所示:
Put each string as you process it in the outputlines
list, unless it matches the value typed in, like this:
Dim lines() As String
Dim outputlines As New List(Of String)
Dim searchString As String = Textbox1.Text
lines = IO.File.ReadAllLines("D:\list.txt")
For Each line As String In lines
If line.Contains(searchString) = False Then
outputlines.Add(line)
End If
Next
现在outputlines
匹配与用户键入的内容不匹配的每一行,您可以将outputlines
列表的内容写入文件.
Now outputlines
matches every line that did not match what was typed in by the user and you can write the contents of the outputlines
list to file.
这篇关于从文本文件vb.net删除一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!