javascript - How To Break A Group of Words In A Line Into 5 lines of words -


the string example "this long sentence need break 5 lines no matter how hard try cannot seem work on website need thank you"

result should be:

1 row - max 50 characters per row

"this long sentence need break"

2 row - max 50 characters per row

"into 5 lines no matter how hard try i"

3 row - max 50 characters per row

"cannot seem work on website i"

4 row - max 50 characters per row

"need thank you"

5 row - max 50 character it's blank hit finish string.

=============

here script have now

var string = "spand span spand span"; var arr = string.split(" "); var unique = []; var arrcount = []; $.each(arr, function (index,word) {     if ($.inarray(word, unique) === -1)          unique.push(word);         arrcount.push(word.split(" ").length); }); alert(unique+arrcount); 

====== cannot seem figure how able count total string of words , break string after hits 50 characters words , not want break words character.

i found looking :)

function splitline(st,n)  {var b = ''; var s = st;while (s.length > n) {var c = s.substring(0,n);var d = c.lastindexof(' ');var e =c.lastindexof('\n');if (e != -1) d = e; if (d == -1) d = n; b +=       c.substring(0,d) + '\n';s = s.substring(d+1);}return b+s;}  var mytext="this long sentence need break 5 lines no matter how hard try cannot seem work on website need thank you";                   var mytext40 = splitline(mytext,50); var myhtml = mytext40.replace(/\n/g,'<br \/><br \/>');       

this split string lines of @ 50 characters each. if want 5 lines, should use smaller limit -- use maxlen = string.length/5 spread.

var rows = []; var maxlen = 50; var arr = string.split(" "); var currow = arr[0]; var rowlen = currow.length; (var = 1; < arr.length; i++) {     var word = arr[i];     rowlen += word.length + 1;     if (rowlen <= maxlen) {         currow += " " + word;     } else {         rows.push(currow);         currow = word;         rowlen = word.length;     } } rows.push(currow); 

given input string, returns:

["this long sentence need break into",  // 49 characters  "5 lines no matter how hard try cannot seem", // 50 characters  "to work on website need thank",  // 49 characters  "you"]                                                // 3 characters 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -