Tuesday, 19 June 2012

Truncate a string without cuting in middle of the word

Hi,

While displaying come content in the web page, we may required to truncate the text to some extent i.e limiting the text certain length.

In past I sued to do as below

> text[0..100]

It used to truncate the text to 100 characters length but the word at the last one will be cut.

rails provides a good helper method for truncate the string without cutting words.

truncate(description, :length => length, :separator => ' ')

This will be very useful while truncate the string.


Thank You,
Uma Mahesh.

No comments:

Post a Comment