What's the easiest way to search for a string within a memory stream (and multiple strings) and return true or false?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
var ms:TMemoryStream;
strS:TStringStream;
aStr:string;
aPos:integer;
found:boolean;
begin
ms:=TMemoryStream.Create;
ms.LoadFromFile('c:\aFile.txt');
strS:=TStringStream.Create;
strS.LoadFromStream(ms);
aPos:=pos(aStr,strS.dataString);
found:=aPos>0;
end;
TStringStream is an often forgetten but very useful tool - easier and safer than messing with pChars, etc.
For multiple searches, either ackwardly loop using pos,substring, etc or use a RegEx.
This code works fine in Delphi XE, although TStringStream is very old - not sure if it is unicode compliant.
(The example is leaky - I left out the finalization code for the sake of brevity)