Einzelnen Beitrag anzeigen

marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#6

Re: String Formatierung

  Alt 14. Jun 2008, 13:02
Hallo,

das geht auch mit benannten Variablen:

Delphi-Quellcode:
function Substitute(const template: string; info: TStrings): string;
var
  iNext, iPos, iLen: Integer;
  bName: Boolean;
  s: string;
begin
  Result := '';
  iPos := 1;
  bName := False;
  while iPos <= Length(template) do
  begin
    iNext := {StrUtils.}PosEx('%', template, iPos);
    iLen := {Math.}IfThen(iNext = 0, Succ(Length(template)) - iPos, iNext - iPos);
    s := Copy(template, iPos, iLen);
    iPos := IfThen(iNext = 0, Succ(Length(template)) {0}, Succ(iNext)); // EDIT
    if bName then
      if s = ''
        then Result := Result + '%'
        else Result := Result + info.values[s]
      else Result := Result + s;
    bName := not bName;
  end;
end;

// Test
procedure TDemoForm.ButtonClick(Sender: TObject);
var
  info: TStrings;
begin
  info := TStringList.Create;
  info.Values['artist'] := 'Don Robertson';
  info.Values['title'] := 'The Happy Whistler';
  Edit.Text := Substitute('%artist% - %title%', info);
  info.Free;
end;
Grüße vom marabu
  Mit Zitat antworten Zitat