Posted By

tomasdev on 02/25/11


Tagged

String break lines split word


Versions (?)

Word breaker (or how to split a string into lines)


 / Published in: JavaScript
 

Parameters: required [string], optional [number of lines].

By default it breaks the string passed into 3 lines.

It takes a string and converts it to an array of strings, splitting the original string into smaller parts (useful for word breaking).

  1. var lineSplit = function(str, lines){
  2. lines = lines || 3;
  3. var strArr = str.split(" "),
  4. cropSize = Math.ceil(strArr.length / lines),
  5. splitted = [];
  6. while(strArr.length){
  7. splitted.push(strArr.splice(0, cropSize).join(" "));
  8. }
  9. return splitted;
  10. };

Report this snippet  

You need to login to post a comment.