检查文件中是否存在给定的字符串 [英] Check whether a given string exists in a file
问题描述
大家好,
是否有命令检查文本
文件中是否存在给定字符串,而无需阅读文件,一次一行和
然后检查给定的字符串?
谢谢。
kd >
推荐答案
" kd" < kd@discussions.microsoft.com> schrieb:
"kd" <kd@discussions.microsoft.com> schrieb:
是否有命令检查文本
文件中是否存在给定字符串,而不必一次读取文件内容,一行一行
然后检查给定的字符串?
Is there a command to check whether a given string is present in a text
file, without having to read the contents of the file, a line at a time
and
then check for the given string?
编号您可以使用''ReadToEnd''将整个文件读成字符串并使用
''InStr''检查模式,但这仅建议用于小的
文件。
-
MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/>
VB< URL:http ://classicvb.org/petition/>
No. You can use ''ReadToEnd'' to read the whole file into a string and use
''InStr'' to check for the pattern, but this is only recommended for small
files.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
" kd" < kd@discussions.microsoft.com> schrieb im Newsbeitrag
新闻:93 ********************************** @ microsof t.com ...
"kd" <kd@discussions.microsoft.com> schrieb im Newsbeitrag
news:93**********************************@microsof t.com...
是否有命令检查文本
文件中是否存在给定字符串,而不必一次读取文件内容
然后检查给定的字符串?
Is there a command to check whether a given string is present in a text
file, without having to read the contents of the file, a line at a time and then check for the given string?
嗯,你可以做类似的事情
私有函数FindInFile (ByVal searchPattern As String,ByVal path As
String)As Boolean
Dim sr As IO.StreamReader
试试
sr =新IO.StreamReader(IO.File.OpenRead(路径))
返回(sr.ReadToEnd.IndexOf(searchPattern)> -1)
Catch ex As Exception
不返回
最后
sr.Close()
结束尝试
结束功能
但我怀疑这会比逐行检查更快,并且如果匹配发生则会损坏
。这可以很容易地测试......
Well, you could do something like
Private Function FindInFile(ByVal searchPattern As String, ByVal path As
String) As Boolean
Dim sr As IO.StreamReader
Try
sr = New IO.StreamReader(IO.File.OpenRead(path))
Return (sr.ReadToEnd.IndexOf(searchPattern) > -1)
Catch ex As Exception
Return Nothing
Finally
sr.Close()
End Try
End Function
but I doubt that this would be faster then checking line by line and
breaking if a match occurs. That could be easily tested though...
KD,
你的意思是文字" text"或.txt。因为你现在得到的答案是.txt。
对于text而言没有* one *方法,因为你不知道
保存文件的格式。
我希望这有帮助吗?
Cor
KD,
Do you mean with text "text" or ".txt". Because the answers you have got now
are for ".txt".
For "text" there is not *one* method, because you don''t know the format of
the saved file.
I hope this helps?
Cor
这篇关于检查文件中是否存在给定的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!