Skip to content

Instantly share code, notes, and snippets.

@b4blue
Created September 16, 2014 20:35
Show Gist options
  • Select an option

  • Save b4blue/819439b100568a683cb5 to your computer and use it in GitHub Desktop.

Select an option

Save b4blue/819439b100568a683cb5 to your computer and use it in GitHub Desktop.
Split sentence into smaller chunks and separate lines
private function breakSentence(str:String, maxLength:int):String {
var finalString:String = "";
if (str.length > maxLength) {
var str_array:Array = str.split(" ");
var sum:int = 0;
for (var n:int = 0; n < str_array.length; n++) {
sum += str_array[n].length + 1;
if (sum > maxLength) {
n--;
sum = 0;
finalString += "\n";
} else {
finalString += str_array[n] + " ";
}
}
} else {
finalString = str;
}
return finalString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment