关于VB编程判断字符和删除特定行问题详解,下面要给大家介绍的是关于关于VB编程判断字符和删除特定行问题的相关解答,比如一个文本里面有:
@123
-1
-5
-10
@234
-2
-4
@345
-3
-8
...
然后我输入一个数字,如果数字为@后面的字符串就删除@那行和下个@之前的开头为-的那些行。
比如我输入“234”
文本就变成
@123
-1
-5
-10
@345
-3
-8
...
在窗体中添加下列代码并画上按钮控件command1
Private Sub Command1_Click()
Dim FileRead As Integer
Dim FileWrite As Integer
Dim Str As String
Dim flag1, flag2 As Boolean
flag1 = False
flag2 = False
FileRead = FreeFile
If Dir(App.Path & "\" & "testfile.txt") <> "" Then
Open App.Path & "\" & "testfile.txt" For Input As FileRead
FileWrite = FreeFile
Open App.Path & "\" & "Tmp.txt" For Output As FileWrite
Do While Not EOF(FileRead)
If flag1 = False Then
Input #FileRead, Str
If InStr(Str, "@" & Text1.Text) <> 0 Then
flag1 = True
flag2 = False
Else
Print #FileWrite, Str
End If
Else
Input #FileRead, Str
If InStr(Str, "@") <> 0 And InStr(Str, "@" & Text1.Text) = 0 Then
flag2 = True
ElseIf InStr(Str, "@" & Text1.Text) <> 0 Then
flag1 = True
flag2 = False
End If
If flag2 = True Then
Print #FileWrite, Str
End If
End If
Loop
Close FileRead
Close FileWrite
Kill App.Path & "\" & "testfile.txt"
Name App.Path & "\" & "Tmp.txt" As App.Path & "\" & "testfile.txt"
Else
MsgBox "文件不存在!"
End If
End Sub
本人测试通过。能实现你说的功能, 这个代码主要是用临时文件来实现删除特定行的