Board logo

标题: VBS读取某个文本内容来判断是否存在某个字符怎写? [打印本页]

作者: hackhd     时间: 2008-4-21 21:11    标题: VBS读取某个文本内容来判断是否存在某个字符怎写?

VBS读取某个文本内容来判断文本里存不存在某个字符怎写?

比如VBS读取了1.txt里的所有内容。

然后判断这里面存不存在。比如 "love" 如果1。TXT里面有love就msgbox "yes"
没有就no
作者: zh159     时间: 2008-4-21 21:37

strA = "love ***"
strB = "Love ***"
If instr(strA,"love") > 0 Then msgbox "yes"   '区分大小写
If instr(1,strB,"love",1) > 0 Then msgbox "yes"   '不区分大小写

作者: hackhd     时间: 2008-4-21 22:35
谢谢。
作者: hackhd     时间: 2008-4-21 23:52
strA = "l*********"
If instr(strA,"love") < 0 Then msgbox "no"   '区分大小写
这样就不弹哦
作者: zh159     时间: 2008-4-22 00:03
InStr 函数:返回某字符串在另一字符串中第一次出现的位置。
也就是最小为0,“0” < 0不成立,也就不能执行后面的命令
strA = "l*********"
If instr(strA,"love") = 0 Then msgbox "no"

作者: abcd     时间: 2008-4-22 00:19
楼主已经说了,读取某个文本,来判断,只能是用正则了。
作者: zh159     时间: 2008-4-22 01:40


  Quote:
Originally posted by abcd at 2008-4-22 00:19:
楼主已经说了,读取某个文本,来判断,只能是用正则了。

试试看
VBS
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("a.txt",1)
str = f.ReadAll
f.Close

If instr(str,"love") = 0 Then msgbox "no"
If instr(str,"love") > 0 Then msgbox "yes"
a.txt
asdas
asd
love

作者: abcd     时间: 2008-4-22 11:39


  Quote:
Originally posted by zh159 at 2008-4-22 01:40 AM:


试试看
VBS
[code]Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("a.txt",1)
str = f.ReadAll
f.Close

If instr(str,"love") = ...

这种用法处理不了特殊字符
作者: zh159     时间: 2008-4-22 11:42


  Quote:
Originally posted by abcd at 2008-4-22 11:39:


这种用法处理不了特殊字符

请问是哪些特殊字符?

~!@#$%^&*()_+|?><?
作者: abcd     时间: 2008-4-22 11:55
双引号
作者: zh159     时间: 2008-4-22 12:05


  Quote:
Originally posted by abcd at 2008-4-22 11:55:
双引号

双引号在VBS里面要用两个
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("a.txt",1)
str = f.ReadAll
f.Close

var = """"

If instr(str,var) = 0 Then msgbox "no"
If instr(str,var) > 0 Then msgbox "yes"
a.txt
asdas
asd
love
"
回车、换行符号都可以查找
作者: abcd     时间: 2008-4-22 12:19
谢谢,俺孤陋了。