you are in: codestackercodes [RSS]trim a string in words

trim a string in words Delicious Email

show/hide lines
   1  def trim_by_words(string,wordcount) 
   2    string.split[0..(wordcount-1)].join(" ") +(string.split.size > wordcount ? "..." : "") 
   3  end
   4  
   5  # example
   6  string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
   7  
   8  puts trim_by_words(string,10) # returns: Lorem Ipsum is simply dummy text of the printing and...
created by leozera — 14 December 2008 — get a short url — tags: ruby string embed

Comments



We dont send spam :)

If checked, it's highlights your comment
simple_captcha.jpg
Are you a human? Type the code from the image