VB 替换字符与处理问题.
192.168.58.60:135
192.168.58.50:135
192.168.58.40:135
192.168.58.30:135
192.168.58.20:135
192.168.58.10:135
如何将字符串中的:135去掉?也就是说如何将:包括:后面的东西去掉呢?
我是这样实现的,
TextZL = " "
Dim q As Integer, w As String, e() As String
e() = Split(IP_JieGuo, vbCrLf)
For q = 0 To UBound(e)
w = e(q)
If w = " " Then
Exit For
Else
r = Left(w, InStrRev(w, ": ")) + vbCrLf
r = Replace(r, ": ", " ")
TextZL = TextZL + r
IP_JieGuo = TextZL
End If
Next q
但结果是点一次,确实是实现了去掉:135了,但在点一次就都变回车了...
TextZL = " " 我觉得不是这个原因,因为我是用两个TEXT来实现的,我知道我的代码很垃圾,如果有高手会的请告诉下小弟了,先谢谢了..
还有个问题,就是处理数据的,
有一个文本文件里有着这样的内容..
-------------------------------------------
Performing Time: 8/26/2007 4:5:23 --> Normal Scan: About To Scan 65536 IP Using 512 Threads
LastIP Scanned: 218.25.20.23:135
Scan 5142 IPs Complete In 0 Hours 0 Minutes 33 Seconds. Found 0 Hosts
-------------------------------------------
-------------------------------------------
Performing Time: 8/27/2007 5:22:34 --> Normal Scan: About To Scan 65535 Ports Using 512 Thread
127.0.0.1 80 Open
127.0.0.1 110 Open
127.0.0.1 135 Open
127.0.0.1 443 Open
LastIP Scanned: 127.0.0.1:518
Scan 127.0.0.1 Complete In 0 Hours 0 Minutes 3 Seconds. Found 4 Open Ports
-------------------------------------------
我只想要其中的
127.0.0.1 80 Open
127.0.0.1 110 Open
127.0.0.1 135 Open
127.0.0.1 443 Open
这个部分,然后处理成
127.0.0.1:80
127.0.0.1:110
127.0.0.1:135
127.0.0.1:443
如上这个样子,我刚刚接触VB很多东西都得学习,请大家帮帮忙。
[解决办法]
不可以 replace(TextZL, ":135 ", " "),因为端口号是不确定的,下一次可能就不是 135 了。
TextZL = " "
Dim q As Integer, e() As String
e() = Split(IP_JieGuo, vbCrLf)
For q = 0 To UBound(e)
If e(q) > " " Then
TextZL = TextZL & IIf(Len(TextZL) > " ", vbCrLf, " ") & _
Left(e(q), InStrRev(e(q), ": ") - 1)
End If
Next q
IP_JieGuo = TextZL
[解决办法]
更正:
TextZL = TextZL & IIf(TextZL > " ", vbCrLf, " ") & _
Left(e(q), InStrRev(e(q), ": ") - 1)
问题2
Dim strLine As String
Open "c:\你的文件.txt " For Input As #1
Do Until EOF(1)
Line Input #1, strLine
If Val(strLine) And Instr(strLine, ". ") Then
Text1 = Text1 & IIf(Text > " ", vbCrLf, " ") & strLine
End If
Loop
Close #1
[解决办法]
问题1 : 去掉 : 号 和后面的 数据:
思路 使用instr 提取出 :号的位置 再以left 提取整个字符串:号前的字符就可以了!
问题 :提取IP 地址 :
分析: 你要提取的那段字符 需要提取的部分与要去掉部分的区别在于 开头字符
取消部分开头为英文 保留部分开头为数字
解决办法:判断每一行字符的第一个字符 若为数字保留这段不是数字的就取消;
得到保留行后 判断 该行第一个空格的位置 使用 left 提取出就可以得到你想要的了
=============================
判断英文或数字可以根据 asc码来判断
[解决办法]
1.
Dim s As String
Dim sTmp() As String
Dim i As Integer
s = "192.168.58.60:135 " & vbCrLf & _
"192.168.58.50:135 " & vbCrLf & _
"192.168.58.40:135 " & vbCrLf & _
"192.168.58.30:135 " & vbCrLf & _
"192.168.58.20:135 " & vbCrLf & _
"192.168.58.10:135 "
sTmp = Split(s, vbCrLf)
s = " "
For i = 0 To UBound(sTmp)
s = s & Split(sTmp(i), ": ")(0) & vbCrLf
Next
MsgBox s
2.
Dim strLine As String
Dim str As String
Dim sTmp() As String
Dim i As Integer
Open "d:\test.txt " For Input As #1
While Not EOF(1)
Line Input #1, strLine
If Val(strLine) <> 0 Then
sTmp = Split(strLine, Chr(32))
strLine = sTmp(0)
For i = 1 To UBound(sTmp)
If Val(sTmp(i)) <> 0 Then
strLine = strLine & ": " & sTmp(i)
End If
Next
str = str & strLine & vbCrLf
End If
Wend
Close #1
MsgBox str
2勉强可用,感觉不是太爽,呵呵...............
[解决办法]
问题一:
temp = "192.168.58.60:135 "
temp = Mid$(temp1, 1, InStr(temp1, ": ") - 1)
多行可以采用循环
问题二:
temp = "127.0.0.1 80 Open "
if instr(temp, "Open ") > 1 then '判断条件包含open
i = InStr(temp, " ") '取得第一个字段结束位置
temp1 = Mid$(temp, 1, i - 1) '纪录第一字段127.0.0.1
temp = Trim(Mid$(temp, i, Len(temp))) '纪录剩余字段80 Open
i = InStr(temp, " ") '取得第二个字段结束位置
temp = temp1+ ": "+Mid$(temp, 1, i - 1) '第一字段+第二字段
End If
多行可以采用循环,不包含Open的直接丢弃
以上数据源如果是文件,采用行行输入家循环即可