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


/ Published in: JavaScript
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.