replicate ... (string function)

Prev Next

Function Names

replicate, replicate characters

Description

The function replicate() repeats the specified string by the number provided.

  • If the 2nd parameter is 0 or negative, then a blank string is returned.
  • If the 2nd parameter is 1, then the original string will be returned.
  • If the 2nd parameter is 2, thenthe contents will be repeated once, and so forth.

The function replicate characters() repeats the specified string until the specified number of characters has been reached.

  • If the 2nd parameter is 0 or negative, then a blank string is returned.
  • If the 2nd parameter is 1, then 1 character is returned
  • If the 2nd parameter is a multiple of the length of the string, then the string wil be replicated until the specified length has been reached.
  • If the specified number is not a multiple of the number of characters of the string, then the resulting string ends with a truncated part of the original string.

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
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 Replication or character count

Specifies the number of repetitions or characters in the final string

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( replicate( Café, 10 ) );
               echo( replicate characters( Café, 10 ) );
               echo( replicate ( { Café, Caffè }, 5 ) );       // Vectorization

Output

CaféCaféCaféCaféCaféCaféCaféCaféCaféCafé
CaféCaféCa
{'CaféCaféCaféCaféCafé','CaffèCaffèCaffèCaffèCaffè'}
Try it yourself: Open LIB_Function_replicate.b4p in B4P_Examples.zip. Decompress before use.

See also

pad