class pclTextBox::Format


Functions for formatting or rearranging text.


static Str numStr(Num number, Int width := 0, Int precision := 0)

Formats the given number into a string.

  • width specifies the width of the final string: left-padded if positive, or right-padded if negative.
  • precision specifies the number of decimal points to display.


Format.numStr(3, 4)           => "   3"
Format.numStr(-3.123f, -8, 2) => "-3.12   "

static Void show(Str formatStr, Obj?[] arguments := Obj?[,])

Takes a format string containing a number of template markers and displays to current out a string with the markers filled with a representation of the corresponding argument.

Template markers include:

~[w]a     writes its argument using toStr with optional width
~[w[,d]]F writes a numerical argument with optional width and precision
~~        writes a tilde

The optional width will pad its argument left (if positive) or right (if negative).

It is an error if a non-numerical argument is provided for the ~F marker, or if there are insufficient arguments to cover all markers in the format string.

Examples"hello ~a", ["world"])            => "hello world""~5a = ~10,5F", ["pi", Float.pi]) => "   pi =    3.14159"

static Void showOut(OutStream out, Str formatStr, Obj?[] arguments := Obj?[,])

Same as show but writes output to the given OutStream


static Str showStr(Str formatStr, Obj?[] arguments := Obj?[,])

Same as show but returns the output as a Str


static Str[] wordWrap(Str text, Int width)

Given a string and a target width, the algorithm returns a list of strings, each string representing a line with words wrapped based on the target width. Word wrap uses a greedy algorithm, minimising the number of returned lines.


static Str wordsWithCommas(Str[] words, Bool oxfordComma := false)

Returns a string with the given words separated by commas and "and" before the last item. Optional argument is a flag to indicate if an "Oxford comma" should be inserted before "and" for three or more items.