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

Invert GMLscripts.com

string_limit

Returns a given string, truncated if it exceeds a certain length. If truncated, an extension (such as an ellipsis) can be appended.

//  string_limit(str, len, ext)
s = string_limit("this is long text", 10, "...");   //  "this is..."
s = string_limit("this is long text", 10);          //  "this is lo"
s = string_limit("this is long text", 1);           //  "t"
s = string_limit("this is long text", 1, "...");    //  "..."
string_limit(str, len, ext)
Returns a given string, truncated if it exceeds a certain length.
COPY/// @func   string_limit(str, len, ext)
///
/// @desc   Returns a given string, truncated if it exceeds a certain length.
///         If truncated, an extension (such as an ellipsis) can be appended.
///
/// @param  {string}    str         text
/// @param  {real}      len         character length
/// @param  {string}    ext         text to append, optional
///
/// @return {string}    truncated string
///
/// GMLscripts.com/license

function string_limit(str, len, ext = "")
{
    var wid = max(len, string_length(ext));
    if (string_length(str) <= wid) {
        return str;
    }
    while (string_length(str + ext) > wid) {
        str = string_delete(str, string_length(str), 1);
    }
    return str + ext;
}

Contributors: xot

GitHub: View · Commits · Blame · Raw