Autor Beitrag
martin300
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 186
Erhaltene Danke: 2



BeitragVerfasst: Fr 03.04.09 14:50 
Hallo,
ich möchte einen Text möglichst flexibel gestalten. Dazu soll der erste Teil der Formatanweisung geändert werden. Da es nur ein String ist, sind Änderungen kein Problem. Die übergebenen Argumente sind in einem Konstanten Array. Und da ist mein Problem. Wie übergebe ich das Array möglichst flexibel. (dh. Änderung der Anzahl u. Werte der Parameter).

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
var
    x,y: String;
begin
  x:= 'Text';
  y:= 'länger';
  label1.Caption:=  format('Hier steht ein %s der auch %s sein kann.',[x,y]);
end;



Moderiert von user profile iconNarses: Topic aus Open Source Projekte verschoben am Fr 03.04.2009 um 15:33
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 03.04.09 15:02 
Der Trick besteht bei Formatstrings darin, nicht die Parameter-Reihenfolge zu ändern, sondern dem Format-String zu sagen, dass die Argumente in einer anderen Reihenfolge wie übergeben genutzt werden müssen.

Siehe dazu auch die Beschreibung von Formatstrings und den zusätzlichen Modifiern. '%1:s' dürfte für dich interessant sein ....

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
martin300 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 186
Erhaltene Danke: 2



BeitragVerfasst: Fr 03.04.09 15:19 
Das war schon hilfreich. Somit ändert sich die Reihenfolge der Parameter. Und wie definiere ich dazu ein variables Konstanten Array? (Das ist eigentlich schon ein Widerspruch) um die Anzahl der Parameter zu ändern? Bzw. gibt es eine bessere Lösung?
Ich könnte natürlich auch Variable und Konstante umdrehen.....
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Fr 03.04.09 15:28 
Du kannst Format ein Suche in der Entwickler-Ecke DYNAMISCHES ARRAY übergeben:

ausblenden Delphi-Quelltext
1:
var DynamnicArray: array of String;					

Und mit SetLength kannst du die Länge des Strings setzen. :idea:
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Fr 03.04.09 15:42 
Du kannst dem Format ein array of const (= array of TVarRec) übergeben, dieses Array kannst du dynamisch zur Laufzeit zusammensetzen wie du willst.


Zuletzt bearbeitet von delfiphan am Fr 03.04.09 15:43, insgesamt 1-mal bearbeitet
martin300 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 186
Erhaltene Danke: 2



BeitragVerfasst: Fr 03.04.09 15:43 
Ein dynamisches Array kann aber nur als erster Parameter übergeben werden. Oder?
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Fr 03.04.09 15:44 
Nein, der zweite Parameter ist ein Array. Der erste ist ein String.

ausblenden Delphi-Quelltext
1:
function Format(const Format: stringconst Args: array of const): string;					

Beispiel:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
var
  Args: array of TVarRec;
  S: string;
begin
  SetLength(Args, 1);
  Args[0].VType := vtInteger;
  Args[0].VInteger := 5;
  S := 'The number is: %d';
  ShowMessage(Format(S,Args)); // The number is: 5
end;
martin300 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 186
Erhaltene Danke: 2



BeitragVerfasst: Fr 03.04.09 15:58 
Danke. Mit dem Code müsste es funktionieren.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 03.04.09 17:30 
Was ich meinte, und was häufig auch in Lokalisierungsengines verwendet wird, ist das Index-Feature:

Vergleiche:
ausblenden Delphi-Quelltext
1:
  Caption := Format('And %1:s said "%0:s"', ['Hello World!''HAL 9000']);					

ausblenden Delphi-Quelltext
1:
  Caption := Format('"%0:s" sagte %1:s', ['Hello World!''HAL 9000']);					

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.