rpad

Right pad a string.

Usage

var rpad = require( '@stdlib/string/base/right-pad' );

rpad( str, len, pad )

Right pads a string such that the padded string has a length of at least len.

var str = rpad( 'a', 5, ' ' );
// returns 'a    '

str = rpad( 'beep', 10, 'b' );
// returns 'beepbbbbbb'

str = rpad( 'boop', 12, 'beep' );
// returns 'boopbeepbeep'

Notes

  • An output string is not guaranteed to have a length of exactly len, but to have a length of at least len. To generate a padded string having a length equal to len

    var str = rpad( 'boop', 10, 'beep' ); // => length 12
    // returns 'boopbeepbeep'
    
    str = str.substring( 0, 10 ); // => length 10
    // returns 'boopbeepbe'
    
    str = str.substring( str.length-10 ); // => length 10
    // returns 'opbeepbeep'
    

Examples

var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var papply = require( '@stdlib/utils/papply' );
var papplyRight = require( '@stdlib/utils/papply-right' );
var naryFunction = require( '@stdlib/utils/nary-function' );
var map = require( '@stdlib/utils/map' );
var logEach = require( '@stdlib/console/log-each' );
var rpad = require( '@stdlib/string/base/right-pad' );

// Define a string to pad:
var str = 'beep';

// Generate random lengths:
var lens = discreteUniform( 10, str.length, str.length+10 );

// Create a function for creating padded strings:
var fcn = naryFunction( papply( papplyRight( rpad, 'b' ), str ), 1 );

// Generate padded strings:
var out = map( lens, fcn );

// Print results:
logEach( '%s', out );
Did you find this page helpful?