Sign in Join
Wiki copytemporary

Extended Format Function/pl

Opis

Moduł efmtf to zestaw klas i procedur umożliwiający formatowanie ciągów tekstowych na podstawie parametrów przekazanych w tablicy, z możliwością dodawania własnych reguł formatowania dla zmiennych obiektowych oraz wsparciem dla języka SQL. Moduł jest nadal zależny od standardowych funkcji FormatDateTime oraz Format.

Przykłady

Formatowanie z adresowaniem zmiennych

pascal
S := Fmt('string: {0:s}, integer: {1:d}, money: {2:m}, float: {2:f}', ['test', 1, 22.3]);
wynik: string: test, integer: 1, money: 22.30 z??, float: 22.3

Adresowanie mieszane i domyślne formatowanie

pascal
S := Fmt('string: {}, integer: {}, money: {:m}, float: {2}', ['test', 1, 22.3]);
wynik: string: test, integer: 1, money: 22.30 z??, float: 22.3

Wsparcie dla SQL (parametry pisane z dużej litery):

pascal
S := Fmt('SELECT * FROM employees WHERE FirstName = {:S} AND DateHired < {:TD};', ['John', Now]);
wynik: SELECT * FROM employees WHERE FirstName = 'John' AND DateHired < '2009-01-01';

Wsparcie dla parametrów globalnych

pascal
SetParam('test', 'World');
S := Fmt('Hello {$test}!');

wynik: Hello World!

Wsparcie dla formatowania zmiennych obiektowych:

pascal
// funkcja formatująca
function FormFmt(AObject: TObject; Mode: Char; Param: string): string;
var
  f: TForm;
begin
  if AObject is TForm then
  begin
    f := TForm(AObject);
    if Param = 'caption' then
      Result := f.Caption
    else
      Result := f.Name;
  end;
end;

// przykład
RegisterFunction(TForm, FormFmt);
S := Fmt('caption: {0:scaption}, name: {0:s}', [Form1]);

Escaped String

pascal
S := Fmt('\{2 + 2\} * 2, {:SE}', ['Hello'#13#10'World!']);

wynik: {2 + 2} * 2, 'Hello\r\nWorld!'

Download

Licencja

Zmiany

Autor


From the Free Pascal wiki, page "Extended Format Function/pl", as it stood on 2020-02-14 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.