VB怎么查找字符,删除掉指定的行
1.txt文本内容%1N1T8D8M6N2S17000N3M8N4G0X0.000Y662.343Z20.000N5G0X0.000Y662.343Z0.200N6G1X...
1.txt文本内容
%1
N1 T8 D8 M6
N2 S17000
N3 M8
N4 G0 X0.000 Y662.343 Z20.000
N5 G0 X0.000 Y662.343 Z0.200
N6 G1 X0.000 Y662.343 Z-3.000 F5000
N7 G1 X376.349 Y662.343 Z-3.000
N8 G0 X376.349 Y662.343 Z20.000
N9 G0 X0.000 Y0.000 Z20.000
N10 M5
N11 M9
N12 T8 D8 M6
N13 S17000
N14 M8
N15 G0 X0.000 Y0.000 Z0.200
N16 G1 X0.000 Y0.000 Z-3.000
N17 G1 X376.349 Y0.000 Z-3.000
N18 G0 X376.349 Y0.000 Z20.000
N19 G0 X0.000 Y0.000 Z20.000
N20 M5
N21 M9
N22 T8 D8 M6
N23 S17000
N24 M8
N25 G0 X0.000 Y109.556 Z20.000
N26 G0 X0.000 Y109.556 Z0.200
N27 G1 X0.000 Y109.556 Z-3.000
N28 G1 X376.349 Y109.556 Z-3.000
N29 G0 X376.349 Y109.556 Z20.000
N30 G0 X0.000 Y0.000 Z20.000
N31 M5
N32 M9
N33 T8 D8 M6
N34 S17000
N35 M8
N36 G0 X0.000 Y206.125 Z20.000
N37 G0 X0.000 Y206.125 Z0.200
N38 G1 X0.000 Y206.125 Z-3.000
N39 G1 X376.349 Y206.125 Z-3.000
N40 G0 X376.349 Y206.125 Z20.000
N41 G0 X0.000 Y0.000 Z20.000
N42 M5
N43 M9
N44 T8 D8 M6
N45 S17000
N46 M8
N47 G0 X0.000 Y281.550 Z20.000
N48 G0 X0.000 Y281.550 Z0.200
N49 G1 X0.000 Y281.550 Z-3.000
N50 G1 X376.349 Y281.550 Z-3.000
N51 G0 X376.349 Y281.550 Z20.000
N52 G0 X0.000 Y0.000 Z20.000
N75 M5
N76 M9
%1
查找这个文件字符S17000,第一个查找到的跳过,查第二个S17000,
把这个字符的上4行和下1行删除掉,也就是6行替换成空的一行,
N9 G0 X0.000 Y0.000 Z20.000
N10 M5
N11 M9
N12 T8 D8 M6
N13 S17000
N14 M8
删除后1.TXT文件内容如下
1.txt文本内容
%1
N1 T8 D8 M6
N2 S17000
N3 M8
N4 G0 X0.000 Y662.343 Z20.000
N5 G0 X0.000 Y662.343 Z0.200
N6 G1 X0.000 Y662.343 Z-3.000 F5000
N7 G1 X376.349 Y662.343 Z-3.000
N8 G0 X376.349 Y662.343 Z20.000
N15 G0 X0.000 Y0.000 Z0.200
N16 G1 X0.000 Y0.000 Z-3.000
N17 G1 X376.349 Y0.000 Z-3.000
N18 G0 X376.349 Y0.000 Z20.000
N25 G0 X0.000 Y109.556 Z20.000
N26 G0 X0.000 Y109.556 Z0.200
N27 G1 X0.000 Y109.556 Z-3.000
N28 G1 X376.349 Y109.556 Z-3.000
N29 G0 X376.349 Y109.556 Z20.000
N36 G0 X0.000 Y206.125 Z20.000
N37 G0 X0.000 Y206.125 Z0.200
N38 G1 X0.000 Y206.125 Z-3.000
N39 G1 X376.349 Y206.125 Z-3.000
N40 G0 X376.349 Y206.125 Z20.000
N47 G0 X0.000 Y281.550 Z20.000
N48 G0 X0.000 Y281.550 Z0.200
N49 G1 X0.000 Y281.550 Z-3.000
N50 G1 X376.349 Y281.550 Z-3.000
N51 G0 X376.349 Y281.550 Z20.000
N52 G0 X0.000 Y0.000 Z20.000
N75 M5
N76 M9
%1 展开
%1
N1 T8 D8 M6
N2 S17000
N3 M8
N4 G0 X0.000 Y662.343 Z20.000
N5 G0 X0.000 Y662.343 Z0.200
N6 G1 X0.000 Y662.343 Z-3.000 F5000
N7 G1 X376.349 Y662.343 Z-3.000
N8 G0 X376.349 Y662.343 Z20.000
N9 G0 X0.000 Y0.000 Z20.000
N10 M5
N11 M9
N12 T8 D8 M6
N13 S17000
N14 M8
N15 G0 X0.000 Y0.000 Z0.200
N16 G1 X0.000 Y0.000 Z-3.000
N17 G1 X376.349 Y0.000 Z-3.000
N18 G0 X376.349 Y0.000 Z20.000
N19 G0 X0.000 Y0.000 Z20.000
N20 M5
N21 M9
N22 T8 D8 M6
N23 S17000
N24 M8
N25 G0 X0.000 Y109.556 Z20.000
N26 G0 X0.000 Y109.556 Z0.200
N27 G1 X0.000 Y109.556 Z-3.000
N28 G1 X376.349 Y109.556 Z-3.000
N29 G0 X376.349 Y109.556 Z20.000
N30 G0 X0.000 Y0.000 Z20.000
N31 M5
N32 M9
N33 T8 D8 M6
N34 S17000
N35 M8
N36 G0 X0.000 Y206.125 Z20.000
N37 G0 X0.000 Y206.125 Z0.200
N38 G1 X0.000 Y206.125 Z-3.000
N39 G1 X376.349 Y206.125 Z-3.000
N40 G0 X376.349 Y206.125 Z20.000
N41 G0 X0.000 Y0.000 Z20.000
N42 M5
N43 M9
N44 T8 D8 M6
N45 S17000
N46 M8
N47 G0 X0.000 Y281.550 Z20.000
N48 G0 X0.000 Y281.550 Z0.200
N49 G1 X0.000 Y281.550 Z-3.000
N50 G1 X376.349 Y281.550 Z-3.000
N51 G0 X376.349 Y281.550 Z20.000
N52 G0 X0.000 Y0.000 Z20.000
N75 M5
N76 M9
%1
查找这个文件字符S17000,第一个查找到的跳过,查第二个S17000,
把这个字符的上4行和下1行删除掉,也就是6行替换成空的一行,
N9 G0 X0.000 Y0.000 Z20.000
N10 M5
N11 M9
N12 T8 D8 M6
N13 S17000
N14 M8
删除后1.TXT文件内容如下
1.txt文本内容
%1
N1 T8 D8 M6
N2 S17000
N3 M8
N4 G0 X0.000 Y662.343 Z20.000
N5 G0 X0.000 Y662.343 Z0.200
N6 G1 X0.000 Y662.343 Z-3.000 F5000
N7 G1 X376.349 Y662.343 Z-3.000
N8 G0 X376.349 Y662.343 Z20.000
N15 G0 X0.000 Y0.000 Z0.200
N16 G1 X0.000 Y0.000 Z-3.000
N17 G1 X376.349 Y0.000 Z-3.000
N18 G0 X376.349 Y0.000 Z20.000
N25 G0 X0.000 Y109.556 Z20.000
N26 G0 X0.000 Y109.556 Z0.200
N27 G1 X0.000 Y109.556 Z-3.000
N28 G1 X376.349 Y109.556 Z-3.000
N29 G0 X376.349 Y109.556 Z20.000
N36 G0 X0.000 Y206.125 Z20.000
N37 G0 X0.000 Y206.125 Z0.200
N38 G1 X0.000 Y206.125 Z-3.000
N39 G1 X376.349 Y206.125 Z-3.000
N40 G0 X376.349 Y206.125 Z20.000
N47 G0 X0.000 Y281.550 Z20.000
N48 G0 X0.000 Y281.550 Z0.200
N49 G1 X0.000 Y281.550 Z-3.000
N50 G1 X376.349 Y281.550 Z-3.000
N51 G0 X376.349 Y281.550 Z20.000
N52 G0 X0.000 Y0.000 Z20.000
N75 M5
N76 M9
%1 展开
2个回答
展开全部
给你一段简单易懂的,且结果中没有多余的空行:
Private Sub Command1_Click()
'读取文件内容
Dim LoadBytes() As Byte
Open "E:\1.txt" For Binary As #1
ReDim LoadBytes(1 To LOF(1))
Get #1, , LoadBytes
Close #1
arr = Split(StrConv(LoadBytes, vbUnicode), vbCrLf)
'查找第一次出现S17000的位置
For i1 = 0 To UBound(arr)
If InStr(arr(i1), "S17000") > 0 Then Exit For
Next
'从第一次出现S17000的位置的下一个位置开始
'找到S17000,从当前向前查4个变为Chr(27)即ESC键,此字符在文本文件中是不可能出现的
For i2 = i1 + 1 To UBound(arr)
If InStr(arr(i2), "S17000") > 0 Then
For j = i2 - 4 To i2
If j >= i1 Then arr(j) = Chr(27)
Next
If j <= UBound(arr) Then arr(j) = ""
End If
Next
Open "E:\2.txt" For Output As #1
Print #1, Replace(Join(arr, vbCrLf), Chr(27) & vbCrLf, "")
Close #1
End Sub
Private Sub Command1_Click()
'读取文件内容
Dim LoadBytes() As Byte
Open "E:\1.txt" For Binary As #1
ReDim LoadBytes(1 To LOF(1))
Get #1, , LoadBytes
Close #1
arr = Split(StrConv(LoadBytes, vbUnicode), vbCrLf)
'查找第一次出现S17000的位置
For i1 = 0 To UBound(arr)
If InStr(arr(i1), "S17000") > 0 Then Exit For
Next
'从第一次出现S17000的位置的下一个位置开始
'找到S17000,从当前向前查4个变为Chr(27)即ESC键,此字符在文本文件中是不可能出现的
For i2 = i1 + 1 To UBound(arr)
If InStr(arr(i2), "S17000") > 0 Then
For j = i2 - 4 To i2
If j >= i1 Then arr(j) = Chr(27)
Next
If j <= UBound(arr) Then arr(j) = ""
End If
Next
Open "E:\2.txt" For Output As #1
Print #1, Replace(Join(arr, vbCrLf), Chr(27) & vbCrLf, "")
Close #1
End Sub
展开全部
搞定了,见代码:
Private Sub Command2_Click()
Dim TempFile As Long
Dim LoadBytes() As Byte
TempFile = FreeFile
Open "D:\test\123.txt" For Binary As #TempFile
ReDim LoadBytes(1 To LOF(TempFile)) As Byte
Get #TempFile, , LoadBytes
Close TempFile
Dim strAll As String
Dim strArr() As String
Dim newStr() As String
Dim index As Integer
Dim j As Integer
Dim k As Integer
index = 0
j = 0
k = -1
strAll = StrConv(LoadBytes, vbUnicode)
strArr = Split(strAll, vbCrLf)
ReDim newStr(UBound(strArr) - 6)
For i = 0 To UBound(strArr)
If InStr(strArr(i), "S17000") > 0 Then
If index = 0 Then
index = index + 1
Else
k = i + 1
j = j - 4
strArr(i) = ""
End If
End If
If i <> k Then
newStr(j) = strArr(i)
j = j + 1
End If
Next
strAll = ""
For i = 0 To UBound(newStr)
If i = 0 Then
strAll = newStr(i)
Else
strAll = strAll + vbCrLf + newStr(i)
End If
Next
MsgBox strAll
End Sub
Private Sub Command2_Click()
Dim TempFile As Long
Dim LoadBytes() As Byte
TempFile = FreeFile
Open "D:\test\123.txt" For Binary As #TempFile
ReDim LoadBytes(1 To LOF(TempFile)) As Byte
Get #TempFile, , LoadBytes
Close TempFile
Dim strAll As String
Dim strArr() As String
Dim newStr() As String
Dim index As Integer
Dim j As Integer
Dim k As Integer
index = 0
j = 0
k = -1
strAll = StrConv(LoadBytes, vbUnicode)
strArr = Split(strAll, vbCrLf)
ReDim newStr(UBound(strArr) - 6)
For i = 0 To UBound(strArr)
If InStr(strArr(i), "S17000") > 0 Then
If index = 0 Then
index = index + 1
Else
k = i + 1
j = j - 4
strArr(i) = ""
End If
End If
If i <> k Then
newStr(j) = strArr(i)
j = j + 1
End If
Next
strAll = ""
For i = 0 To UBound(newStr)
If i = 0 Then
strAll = newStr(i)
Else
strAll = strAll + vbCrLf + newStr(i)
End If
Next
MsgBox strAll
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询