std.strpad

STRINGstd.strpadSTRINGsINTEGERwidthSTRINGpad

Available inall subroutines.

Constructs a string containing the input string s, padded out with pad to produce a string of the given width. The padding string pad is repeated as necessary, and cut short when width is reached.

Note that width is given in bytes, and this function will cut short paddings with multi-byte encodings.

Negative width left-justifies s by placing padding to the right. Positive width right-justifies s by placing padding to the left. If width is less than or equal to the length of s, then no padding is performed.

If pad is the empty string, then no padding is performed, and the unmodified string s is returned.

Examples

set var.s = std.strpad("abc", -10, "-="); # produces "abc-=-=-=-"
set var.s = std.strpad("abc", 10, "-="); # produces "-=-=-=-abc"

Repeating a string:

declare local var.s STRING;
declare local var.n INTEGER;
set var.n = std.strlen("abcd");
set var.n *= 3;
set var.s = std.strpad("", var.n, "abcd"); # repeat "abcd" three times

User contributed notes

BETA

Do you see an error in this page? Do you have an interesting use case, example or edge case people should know about? Share your knowledge and help people who are reading this page! (Comments are moderated; for support, please contact Fastly support)