Posted By

psradich111 on 08/22/11


Tagged

javascript trim String count length - word BY


Versions (?)

Javascript - Trim string length by word count


 / Published in: JavaScript
 

Trim a string to a defined number of words

  1. var numberOfWords = 4;
  2. function trimByWord(sentence) {
  3. var result = sentence;
  4. var resultArray = result.split(" ");
  5. if(resultArray.length > numberOfWords){
  6. resultArray = resultArray.slice(0, numberOfWords);
  7. result = resultArray.join(" ") + " ...";
  8. }
  9. return result;
  10. }

Report this snippet  

You need to login to post a comment.