Skip to content

Documentation / base / formatString

Function: formatString()

formatString(format, ...args): string

Defined in: libs/base/src/utils.ts:830

Simple sprintf implementation:

Parameters

format

string

The format string

args

...SprintfArg[]

The format arguments

Returns

string

The formatted string

Supported format: %% %s %c %d, %i, %u %f %x, %X, %o

Supported flags:

  • (left align)
  • (always show sign for number) (space if no sign for number) 0 (zero pad)

(alternate form, for x/X/o adds 0x/0X/0o prefix if value is non-zero)

  • (width or precision from argument) n$ (explicit argument index, 1-based) .precision for s (max length) .precision for d/i/u/x/X/o (min digits) .precision for f (number of digits after decimal point, default 6)

Example

ts
formatString('Hello %s', 'World'); // 'Hello World'
formatString('Hex: %#x', 255); // 'Hex: 0xff'
formatString('Width: %*d', 5, 42); // 'Width:    42'
formatString('Pi: %.2f', Math.PI); // 'Pi: 3.14'
formatString('Index: %2$s %1$s', 'first', 'second'); // 'Index: second first'

Released under the MIT License.