判断一字符串是否在一个文件中存在?
请各位大侠帮助一下,在下非常感谢!
急!急!急!
请各位大侠帮助一下,在下非常感谢!
急!急!急!
解决方案 »
- 代理上网,无法连接远程服务器上的MSSQL,我用ADSL拨号上网就可以
- Function声明部分 添加在哪里??奇怪!!
- 怎样在程序中响应一些功能键?
- 关于多人操作同一条数据的问题...
- 请问怎么得到这样格式的日期20030422
- 有哪位兄弟知道水晶报表9.0(Crystal)企业版中的那个Report Application Server (RAS)的密码是多少??高分求救。
- delphi里的一个菜鸟小问题?
- TwwDBComboBox 控件问题;
- object_id('tempdb..#tempTable') is not null 转DELPHI
- fastreport强制换页的问题(急啊)
- 指教马上给分!!!
- 关于SQL-Server自定义函数的问题(急!!!)
procedure TForm1.Button1Click(Sender: TObject);
var
wj:File;
fb:String[20];
br:integer;
begin
assignfile(wj,'文件名');
reset(wj,1);
while not eof(wj) do begin
blockread(wj,fb,sizeof(fb),br);
if pos('指定字符',fb)>0 then Button1.Caption:='存在';
end;
Closefile(wj);
end;
begin
Result := False;
if not FileExist(FileName)then
Exit;
with TStringList.Create do
try
LoadFromFile(FileName);
if Pos(str, Text) > 0 then
Result := True;
finally
Free;
end;
end;
var
Stream: TStream;
S: string;
begin
Result := False;
if not FileExists(FileName) then
Exit;
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
SetString(S, nil, Stream.Size);
Stream.Read(Pointer(S)^, Size);
if Pos(str, s) > 0 then
Result := True;
finally
Stream.Free;
end;
end;
这样很快了,我测试了一下,6.72兆的文件只要30毫秒左右
var
Stream: TStream;
S: string;
Sum, Start, StrEnd: Integer;
begin
Result := 0;
if not FileExists(FileName) then
Exit;
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
SetString(S, nil, Stream.Size);
Stream.Read(Pointer(S)^, Stream.Size);
Start := 1;
StrEnd := Pos(Str, s);
while StrEnd > 0 do
begin
Inc(Result);
Start := StrEnd + Length(Str) + 1;
s := Copy(S, Start, MaxInt);
StrEnd := Pos(Str, s);
end;
finally
Stream.Free;
end;
end;
模仿delphi的stringreplace函数写的,如果统计的个数多了还是很慢的,还是上面那个文件,如果出现次数为551次则要7秒钟,如果次数少还是挺快的