vb读文件时跳过行

来源:百度知道 编辑:UC知道 时间:2024/05/27 17:42:27
若想跳过含特定字符的行,如“>”,怎么办?
也就是如何判断有没有这个字符。
很简单吧?可是我不会:)
大家对不起,我是新新手,请将代码加入下面的代码中:
Dim linetxt As String, alltxt As String
Open "c:\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, linetxt
alltxt = alltxt & linetxt & vbCrLf
Loop
Close #1
Text1.Text = alltxt
谢了

按行来读取文件时

Open "1.txt" For Input As #1 ' 打开文件。
Do While Not EOF(1) ' 循环至文件尾。
Line Input #1, a ' 读入一行数据并将其赋予某变量。
if instr(a,">")<>0 then print a '如果这一行找不到>,就打印这行
Loop
Close #1

呵呵, 就是楼上说的,不过记得instr找不到时,返回的是0!!

补充:
==================================
Dim linetxt As String, alltxt As String
Open "c:\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, linetxt
if instr(linetxt,">")<>0 then alltxt = alltxt & linetxt & vbCrLf
Loop
Close #1
Text1.Text = alltxt

参考一下这两个函数

Mid 函数

返回 Variant (String),其中包含字符串中指定数量的字符。

Replace函数

返回一个字符串,该字符串中指定的子字符串已被替换成另一子字符串,并且替换发生的次数也是指定的。

可以使用instr方法,instr(string,chars)
返回string中,第一次出现chars的位置,没有则返回-1