Delphi.cz

Český portál Delphi

ClassHelper pro TStrings

Jedná se o helper pro TStrings, umožňuje lepší IndexOf() s ohledem na velká-malá písmenka a také i částečné vyhledávání. V nejnovějších Delphi to určitě už je, ale někomu by se to mohlo hodit.

Pozn (editora): berte to jako inspiraci pro použití Class helpers - je to moc užitečná fíčurka

{*******************************************************}
{                                                       }
{       Strings helper for IndexOf enhacenment          }
{                                                       }
{       Copyright © 2004 Jaro.Benes                   }
{                                                       }
{*******************************************************}

unit jbStringsHelper;
//designed in Delphi 2005, free use for general purpose
interface

uses
  Windows, Classes, SysUtils;

type
  TStringsHelper = class helper for TStrings
  public
    {case sensitive version of indexof}
    function AnsiIndexOf(const AString: string): Integer; overload; inline;
    function AnsiIndexOf(const AString: string; Idx: Integer): Integer; 
overload; inline;
    {like as indexof but can search substring}
    function IndexOfSubString(const SubString: string): Integer; overload; inline;
    {like as indexof but can search substring on next occurence}
    function IndexOfSubString(const SubString: string; Idx: Integer): Integer;
 overload; inline;
  end;

implementation

function TStringsHelper.AnsiIndexOf(const AString: string): Integer;
begin
  Result := AnsiIndexOf(AString, 0);
end;

function TStringsHelper.AnsiIndexOf(const AString: string; 
 Idx: Integer): Integer;
var
  i: Integer;
begin
  if (Count > 0) and (Idx <= (Count - 1)) then
    for i := Idx to Count - 1 do
      if AnsiCompareStr(AString, Strings[i]) = 0 then
        begin
          Result := i;
          Exit;
        end;
  Result := -1;
end;

function TStringsHelper.IndexOfSubString(const SubString: string): Integer;
begin
  Result := IndexOfSubString(SubString, 0);
end;

function TStringsHelper.IndexOfSubString(const SubString: string; 
   Idx: Integer): Integer;
var
  i: integer;
begin
  if (Count > 0) and (Idx <= (Count - 1)) then
    for i := Idx to Count - 1 do
      if Pos(Uppercase(SubString), Uppercase(Strings[i])) > 0 then
        begin
          Result := i;
          Exit;
        end;
  Result := -1;
end;

end.

Datum: 2012-03-20 21:58:00 Tagy: utrzky, class helper

Rozšíření Delphi