Actionscript2: Split String by Line Length With & Without Word-Wrap

  • Home /
  • Blog Posts /
  • Actionscript2: Split string by line length with & without word-wrap

Split string into sentences by max line length.
This uses a character array but doesn’t use word-wrapping.

var personalMessage: String = "You got this far so we reckon that you could be curious enough to learn a little more, we?ll contact you shortly to answer any questions you may have.";
_root.myArray = new Array();
_root.myArray = personalMessage.split("");
_root.mySentences = new Array();
var currentCharCount: Number = 0;
var currentCharItem: Number = 0;
var currentCharString: String = "";
var maxLength: Number = 65;

for (var i = 0; i & lt; _root.myArray.length; i++) {
    if (currentCharCount & lt; maxlength) currentcharstring = "" += "_root.myArray[i];"
    if = ""(currentcharcount = "=maxLength" || = ""
        currentcharitem = "=(_root.myArray.length-1))" {
            = ""
            _root.mysentences.push(currentcharstring); = ""
            currentcharcount = "0;"; = ""
        } = ""
        currentcharcount++; = ""
        currentcharitem++; = ""
        trace(_root.mysentences.length); = ""
        trace(_root.mysentences[0]); = ""
        trace(_root.mysentences[1]); = ""
        trace(_root.mysentences[2]); = "" [ = ""
            code
        ] = ""
        this = ""
        outputs = ""
        the = ""
        following: = "" [code = ""
            lang = "c"
        ] = ""
        3 = ""
        you = ""
        got = ""
        far = ""
        so = ""
        we = ""
        reckon = ""
        that = ""
        could = ""
        be = ""
        curious = ""
        enough = ""
        to = ""
        learn = ""
        a = ""
        little = ""
        more, = ""
        we? ll = ""
        contact = ""
        shortly = ""
        answer = ""
        any = ""
        que = ""
        tions = ""
        may = ""
        have. = ""
        split = ""
        string = ""
        into = ""
        sentences = ""
        by = ""
        max = ""
        line = ""
        length = ""
        and = ""
        use = ""
        word - wrapping. = ""
        var = ""
        personalmessage: string = "You got this far so we reckon that you could be curious enough to learn a little more, we?ll contact you shortly to answer any questions you may have."
        _root.myarray = "new"
        array(); = ""
        ");="
        " _root.mysentences="
        new " currentcharstring:string="
        " maxlength:number="
        60;
        " for="
        " (var="
        " i="
        0;
        " i<_root.myarray.length;="
        " i++="
        " )="
        " (currentcharstring.length<maxlength)="
        " " = ""
        ";="
        " ((currentcharstring.length="
        ">= (maxLength-1)) || (i==(_root.myArray.length-1))) {
        _root.mySentences.push(currentCharString); currentCharString = "";
    }
    else currentCharCount++;
} & lt;
/maxlength)>

trace(_root.mySentences.length);
trace(_root.mySentences[0]);
trace(_root.mySentences[1]);
trace(_root.mySentences[2]);

This outputs the following:

You got this far so we reckon that you could be curious enough
to learn a little more, we?ll contact you shortly to answer
any questions you may have.