Thema: Delphi string inkrementieren

Einzelnen Beitrag anzeigen

blackfin
(Gast)

n/a Beiträge
 
#9

AW: string inkrementieren

  Alt 1. Jun 2011, 10:24
Ich habe dafür eine Funktion, die str_pad von php nachbildet.
Dabei gibt man an, auf wieviel Zeichen der String aufgefüllt werden soll (pad_size), mit welcher zeichenfolge aufgefüllt werden soll(pad_string) und dann noch, ob links oder rechts aufgefüllt werden soll (pad_type 0/1)

Delphi-Quellcode:
// pad a string to pad_size with pad_string characters (php-like)
function str_pad(const Input : string;
                 pad_size : integer;
                 const pad_string : string = '0';
                 const pad_type : Integer = 0) : string ;
var
  i : integer;
  dif : Integer ;
  c: char ;
begin
  Result := Input;
  if Length(Input) < pad_size then
  begin
    dif := pad_size - Length(Input) ;
    SetLength(Result,pad_size) ;

    case pad_type of

        // STR_PAD_LEFT
        0:
          begin
             for i := 1 to dif do
             begin
                 if i <= Length(pad_string) then c := pad_string[i]
                 else c := pad_string[Length(pad_string)] ;
                 Result[i] := c ;
             end;

             try
               for i := dif+1 to pad_size do Result[i] := Input[i-dif] ;
             except
             end;
          end;

        // STR_PAD_RIGHT
        1:
          begin
             for i := Length(Input)+1 to pad_size do
             begin
                 if i-dif <= Length(pad_string) then c := pad_string[i-dif]
                 else c := pad_string[Length(pad_string)] ;
                 Result[i] := c ;
             end;

             try
               for i := 1 to dif do Result[i] := Input[i] ;
             except
             end;
          end;
    end;

  end;
end;
  Mit Zitat antworten Zitat