Delphi: Easiest way to search for string in memorystream

孤人 提交于 2020-01-11 12:33:24

问题


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)



来源:https://stackoverflow.com/questions/15950649/delphi-easiest-way-to-search-for-string-in-memorystream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!