padOrTruncateLeft$

A function that can be used in Formulas.

Syntax and semantics

padOrTruncateLeft$ (string$, newWidth [, pad$])
add characters (selected from pad$) to the left of string$, or remove characters from the left of string$, until the result is precisely newWidth characters long:
assert padOrTruncateLeft$ ("hello", 12, ".") = ".......hello"
assert padOrTruncateLeft$ ("hello", 3, ".") = "llo"
assert padOrTruncateLeft$ ("hello", 0) = ""

If pad$ is not given, the padding will be done with spaces:

assert padOrTruncateLeft$ ("hello", 12) = " hello"
assert padOrTruncateLeft$ ("hello", 3) = "llo"

If characters have to be added, and pad$ contains more than one character, the left edge of pad$ is left-aligned to the resulting string, so that the result will look good if padding is used for visualizing columns:

assert padOrTruncateLeft$ ("hello", 12, ".-;)") = ".-;).-;hello"
assert padOrTruncateLeft$ ("goodbye", 12, ".-;)") = ".-;).goodbye"

Note that newWidth cannot be negative:

asserterror Can never truncate a string down to -3 characters.
a$ = padOrTruncateLeft$ ("hello", -3)

There will be an error message if padding is required but no characters are to be found in pad$:

asserterror Empty pad string.
a$ = padOrTruncateLeft$ ("hello", 12, "")

Links to this page


© Paul Boersma 2024