Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi TrimChars (https://www.delphipraxis.net/83404-trimchars.html)

xaromz 2. Jan 2007 13:21

Re: TrimChars
 
Hallo,
Zitat:

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

PeterPanino 2. Jan 2007 16:14

Re: TrimChars
 
Zitat:

Zitat von xaromz
Hallo,
Zitat:

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

Mhm, und was ist der Vorteil der Verwendung von Set gegenüber der Verwendung eines Char?

xaromz 2. Jan 2007 17:13

Re: TrimChars
 
Hallo,
Zitat:

Zitat von PeterPanino
Mhm, und was ist der Vorteil der Verwendung von Set gegenüber der Verwendung eines Char?

Aussagekräftige Argument-Namen.

Gruß
xaromz


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:55 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz