Delphi:最简单的方法来搜索memorystream中的字符串 [英] Delphi: Easiest way to search for string in memorystream
问题描述
在内存流(和多个字符串)中搜索字符串的最简单方法是什么? pre> 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;
结束
TStringStream是一个经常被遗忘但非常有用的工具 - 比使用pChars等更容易和安全。 / p>
对于多次搜索,使用pos,substring等进行循环循环或使用RegEx。
此代码在Delphi XE中工作正常,尽管TStringStream非常旧 - 不知道是否符合unicode。
(示例是漏洞 - 为了简洁起见,我省略了完成代码)
What's the easiest way to search for a string within a memory stream (and multiple strings) and return true or false?
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)
这篇关于Delphi:最简单的方法来搜索memorystream中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!