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<_root.myArray.length; i++ ) {
        if (currentCharCount<maxLength) currentCharString += _root.myArray[i];
        if (currentCharCount==maxLength || currentCharItem==(_root.myArray.length-1)) {
                _root.mySentences.push(currentCharString);
                currentCharCount = 0;
                currentCharString = "";
        }
        currentCharCount++;
        currentCharItem++;
}

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

This outputs the following:

3
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 que
tions you 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.myArray = personalMessage.split(" ");
_root.mySentences = new Array();
var currentCharString:String = "";
var maxLength:Number = 60;

for (var i=0; i<_root.myArray.length; i++ ) {
        if (currentCharString.length<maxLength) currentCharString += _root.myArray[i] + " ";

        if ((currentCharString.length >= (maxLength-1)) || (i==(_root.myArray.length-1))) {
                _root.mySentences.push(currentCharString);
                currentCharString = "";
        } else currentCharCount++;
}

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

This outputs the following:

3
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.

Share This Article

Related Posts

Comments

There are no comments on this entry.

Trackbacks

There are no trackbacks on this entry.

Add a Comment

Required

Required

Optional

Latest Domains

1. weebly.com
(1 day ago)
2. whpromotions.co.uk
(1 day ago)
3. adsl24.co.uk
(1 day ago)
4. youryoungmodels.net
(1 day ago)
5. enom.com
(1 day ago)
6. dynect.net
(1 day ago)
7. websitewelcome.com
(1 day ago)
8. hostgator.com
(1 day ago)
9. uploading.com
(2 days ago)
10. soccernetghana.com
(2 days ago)

Services

PHP MySQL Apache Flash Air Java C++ Python