GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2016-11-28 11:34:42

DJ Coco
Member
Registered: 2016-09-13
Posts: 5

string_wrap(str, width)

Couldn't find a script that did this, so I wrote my own.
Example project can be downloaded from SendSpace here.

Expand/// string_wrap(str, width)
//
//  Returns the string formatted with automatic linebreaks based on the width
//  Words too long to fit into a line are also given a linebreak
//
//      str         the string to format, string
//      width       width of the formatted string, real
//
/// GMLscripts.com/license

{
    var str = argument0, width = argument1, cur_word = "", saved_pos = 1;
    
    for (var i = 1; i <= string_length(str); i++) {
        cur_word += string_char_at(str, i);
        if (string_char_at(str, i) == " " || i == string_length(str)) {
            if (string_width(string_copy(str, 1, i)) >= width) {
                str = string_insert("#", str, saved_pos);
                str = string_delete(str, saved_pos + 1, 1);
            }
            saved_pos = i;
            cur_word = "";
        }
        if (string_width(cur_word) >= width) {
            str = string_insert("#", str, i);
            cur_word = "";
        }
    }
    
    return str;
}

Offline

#2 2016-11-28 21:17:34

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: string_wrap(str, width)

Thanks for the submission.

There is a similar script here: http://www.gmlscripts.com/script/string_wordwrap

We'll have to see how they compare.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB