Search code examples
stringfunctiongml

Linebreak (#) every X characters


How to make a script that inserts # character every x character?

I already tried to make this by creating a script down below.

This is my script, but it doesn't work, somewhy...

/// string_linebreak(str,w)

var str, w, p, l;

str = argument[0];
w = argument[1];
l = string_length(str);

// Linebreakes
for (p = 0; p <= l; p ++) {
    if (p mod w) {
        string_insert("[@l]", str, p);
    }
}

str = string_replace_all(str, "[@l]", "#");

return str;

I except to get a string with # character every x character.


Solution

  • Try this.

    //string_linebreak(str,w)
    var str = argument0;
    var spacing = argument1;
    var leng = string_length(str);
    var output = "";
    
    var p;
    for (p=1; p<=(leng+1); p++)
    {
        output += string_char_at(str,p);
        if ((p mod spacing)==0)
        {
            output += "#";
        }
    }
    
    return output;
    

    In case you were curious, the problem is that you are trying to iterate through each character but you're also adding characters to the string as you do so.