pad, align ...

Prev Next

Function Names

pad, align, pad right, align right

Description

Both functions pad and align adds padding character until the specified number of characters has been reached. Applicable to pad: If the length of thestring is greater or equal to number of characters, then there will be no action. The function align will, however, remove the last characters in order to meet the target length.
The function names ending with ... right add the padding to the lef of the string, so the contents are aligned to the right. Note: The count will be recognized as an integer and will be rounded down if needed.

Vectorization: This function supports vectorization in the 1st function parameter. Instead of providing a single value, you can provide a set or even a nested set which contain multiple values. The function will then process every value and its return value contains a corresponding set containing all results.

Call as: function

Restrictions

Indirect parameter passing is disabled
Vectorization is allowed in the 1st function parameter

Parameter count

1

Parameters

No.TypeDescription
1
input
string
set
Target string

This is the source string which will be replicated. Alternatively, provide a (nested) set with multiple strings to make use of vectorization.

2
input
numeral character count

Specifies the number of characters the output string shall reach.

Opt. 3
input
string Padding string

This string will be used for padding. If the string contains multiple characters, then the character sequence will be repeated. If the number of padding characters needed is not a simple multiple of the number of characters in the padding string, then an incomplete padding strîng portion will be added at the end to precisely meet the character count.
Note: Empty strings will translate to 1 space symbol.

Default value: 1 space symbol
Opt. 4
input
string Overflow string

Applicable to function pad only: If the string is longer than the character count, then the contents will be replaced by the overflow string. Highly recommended to make sure that the overflow string contains the same number of characters as specified in the 2nd function parameter.

If not overflow string is specified, then this function will return the unchanged string.
Note: Ignored in the align function as the string will be shortened.

Return value

TypeDescription
string
set
Processed string

Specified replication or character count. A set will be returned with the results if vectorization is used.

Examples

       echo("Pad up to reach 20 characters:");
       echo( "'", pad  ( "Hello Wörld", 20, "" ),"'");
       echo( "'", align( "Hello Wörld", 20 ),"'");
       echo( "'", pad  ( "Hello Wörld", 20, '_€__' ),"'");
       echo( "'", align( "Hello Wörld", 20, Café ),"'");
       echo( "'", pad right  ( "Hello Wörld", 20, "" ),"'");
       echo( "'", align right( "Hello Wörld", 20 ),"'");
       echo( "'", pad right  ( "Hello Wörld", 20, '_€__' ),"'");
       echo( "'", align right( "Hello Wörld", 20, Café ),"'");

       echo("Align to 8 characters:");
       echo( "'", pad  ( "Hello Wörld", 8 ),"'");
       echo( "'", align( "Hello Wörld", 8 ),"'");
       echo( "'", pad  ( "Hello Wörld", 8, '_€__' ),"'");
       echo( "'", align( "Hello Wörld", 8, Café ),"'");

       echo("Align to 8 characters, pad: Show overflow:");
       echo( "'", pad  ( "Hello Wörld", 8, '.', '###' ),"'");
       echo( "'", align( "Hello Wörld", 8, '.','###' ),"'");
       echo( "'", pad  ( "Hello Wörld", 8, '_€__', '###' ),"'");
       echo( "'", align( "Hello Wörld", 8, Café, '###' ),"'");

       echo("Vectorization:");
       a[] = { "Hi", "Hello", "Ahoi" };
       echo( pad  ( a[], 3, '.', '###' ) );
       echo( align( a[], 3, '.', '###' ) );

Output

Pad up to reach 20 characters:
'Hello Wörld         '
'Hello Wörld         '
'Hello Wörld_€___€___'
'Hello WörldCaféCaféC'
'         Hello Wörld'
'         Hello Wörld'
'_€___€___Hello Wörld'
'CaféCaféCHello Wörld'
Align to 8 characters:
'Hello Wörld'
'Hello Wö'
'Hello Wörld'
'Hello Wö'
Align to 8 characters, pad: Show overflow:
'###'
'Hello Wö'
'###'
'Hello Wö'
Vectorization:
{'Hi.','###','###'}
{'Hi.','Hel','Aho'}
Try it yourself: Open LIB_Function_pad.b4p in B4P_Examples.zip. Decompress before use.

See also

replicate