Registriert seit: 6. Apr 2005
10.109 Beiträge
|
Re: String Formatierung
14. Jun 2008, 14: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
|
|
Zitat
|