# String Formatting
Tools for complex string formatting and template replacement.
## Advanced Formatting
### Format
The `Format` function provides powerful template-based formatting similar to C's `printf` or Delphi's `Format`.
| Specifier | Description | Example |
| :--- | :--- | :--- |
| `%d` | Decimal Integer | `Format('%d', [42])` -> `42` |
| `%x` | Hexadecimal | `Format('%x', [255])` -> `FF` |
| `%f` | Floating point | `Format('%.2f', [3.1415])` -> `3.14` |
| `%e` | Scientific notation | `Format('%e', [1000])` -> `1.00E+003` |
| `%g` | General number | `Format('%g', [0.00001])` -> `1E-5` |
| `%n` | Number with separators | `Format('%n', [1234.5])` -> `1,234.50` |
| `%s` | String | `Format('Hi %s', ['Bob'])` -> `Hi Bob` |
| `%m` | Money | `Format('%m', [10.5])` -> `$10.50` |
```pascal
var s := Format(
'Total: %d items in %.2f seconds',
[ 10, 1.234 ]
);
PrintLn(s);
// OUTPUT
// Total: 10 items in 1.23 seconds
```
### Pattern Replacement
The `StrReplaceMacros` function (or `.ReplaceMacros` helper) allows for template-style replacements using an array of key-value pairs.
```pascal
var template := 'Hello [NAME], welcome to [PLACE].';
var data := ['NAME', 'Alice', 'PLACE', 'the Compendium'];
var msg := template.ReplaceMacros(data, '[', ']');
PrintLn(msg);
// OUTPUT
// Hello Alice, welcome to the Compendium.
```
String Formatting
Tools for complex string formatting and template replacement.
Advanced Formatting
Format
The Format function provides powerful template-based formatting similar to C's printf or Delphi's Format.
Specifier
Description
Example
%d
Decimal Integer
Format('%d', [42]) -> 42
%x
Hexadecimal
Format('%x', [255]) -> FF
%f
Floating point
Format('%.2f', [3.1415]) -> 3.14
%e
Scientific notation
Format('%e', [1000]) -> 1.00E+003
%g
General number
Format('%g', [0.00001]) -> 1E-5
%n
Number with separators
Format('%n', [1234.5]) -> 1,234.50
%s
String
Format('Hi %s', ['Bob']) -> Hi Bob
%m
Money
Format('%m', [10.5]) -> $10.50
var s := Format('Total: %d items in %.2f seconds',[10,1.234]);
PrintLn(s);
Result
Total: 10 items in 1.23 seconds
Pattern Replacement
The StrReplaceMacros function (or .ReplaceMacros helper) allows for template-style replacements using an array of key-value pairs.
var template :='Hello [NAME], welcome to [PLACE].';var data :=['NAME','Alice','PLACE','the Compendium'];var msg := template.ReplaceMacros(data,'[',']');
PrintLn(msg);