- 人气:
- 放大
- 缩小
- 二维码
- 赞赏
delphi 泛型搜索字符串数组
代码1:
uses StrUtils;
procedure TForm1.FormCreate(Sender: TObject);
var
someArray: TArray;
begin
someArray:=TArray.Create('One','Two','Three');
if MatchStr('Two', someArray) then
ShowMessage('It contains Two');
end;
代码2:
{$APPTYPE CONSOLE}
{$R *.res}
uses
Generics.Defaults,
Generics.Collections,
System.SysUtils;
Var
someArray: TArray;
FoundIndex : Integer;
begin
try
someArray:=TArray.Create('a','b','c');
if TArray.BinarySearch(someArray, 'b', FoundIndex, TStringComparer.Ordinal) then
Writeln(Format('Found in index %d',[FoundIndex]))
else
Writeln('Not Found');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
注意:BinarySearch要求对数组进行排序。