You are currently viewing the GMLscripts.com static mirror. Forum access and script submissions are not available through this mirror.

Invert GMLscripts.com

string_rpad

Returns a string padded on its right to a given length. If the padded length is less than the original length, the original string is returned.

//  string_rpad(str, len, pad)
s = string_rpad("123", 7, "xo");        //  "123xoxo"
s = string_rpad("1234", 7, "xo");       //  "1234xox"
s = string_rpad("1234", 8);             //  "1234    "
s = string_rpad("1234", 8, ".");        //  "1234...."
s = string_rpad("", 8, "123456789");    //  "12345678"
s = string_rpad("123456789", 4, ".");   //  "123456789"
string_rpad(str, len, pad)
Returns a string padded on its right to a given length.
COPY/// @func   string_rpad(str, len, pad)
///
/// @desc   Returns a string padded on its right to a given length.
///         If the padded length is less than the original length,
///         the original string is returned.
///
///         eg. string_rpad("1234", 7, "0") == "1234000"
///
/// @param  {string}    str             string of text
/// @param  {real}      len             desired length
/// @param  {string}    pad             padding string (default " ")
///
/// @return {string}    padded string
///
/// GMLscripts.com/license

function string_rpad(str, len, pad=" ")
{
    var padsize = string_length(pad);
    var padding = max(0, len - string_length(str));
    if (padding <= 0) {
        return str;
    }
    var out = str;
    out += string_repeat(pad, padding div padsize);
    out += string_copy(pad, 1, padding mod padsize);
    out = string_copy(out, 1, len);
    return out;
}

Contributors: xot

GitHub: View · Commits · Blame · Raw