Thema: Delphi TrimChars

Einzelnen Beitrag anzeigen

xaromz

Registriert seit: 18. Mär 2005
1.682 Beiträge
 
Delphi 2006 Enterprise
 

Re: TrimChars

  Alt 2. Jan 2007, 13:21
Hallo,
Zitat von PeterPanino:
Wie würdest du es machen?
So:
Delphi-Quellcode:
type
  TTrimDirection = set of (tdLeft, tdRight);

function PATrimChars(const S: String; CS: TSysCharSet; Direction: TTrimDirection): String;
var
  i: Integer;
  L, Left, Right: Integer;
begin
  L := Length(S);

  Left := 1;
  if tdLeft in Direction then
  begin
    for i := 1 to L do
    begin
      if not (S[i] in CS) then
      begin
        Left := i;
        Break;
      end;
    end;
  end;

  Right := L;
  if dtRight in Direction then
  begin
    for i := L downto 1 do
    begin
      if not (S[i] in CS) then
      begin
        Right := i;
        Break;
      end;
    end;
  end;

  Result := Copy(S, Left, Right - Left + 1);
end;
Gruß
xaromz
I am a leaf on the wind - watch how I soar
  Mit Zitat antworten Zitat