0

I have str='My long string with this text'

I know about truncate str, length: 20, omission: '',but it cuts words

My goal is to keep full word after truncation. Is it possible?

string can be random length

user2322409
  • 814
  • 9
  • 15
  • 4
    You can use separator option. Please visit: http://stackoverflow.com/questions/8714045/truncate-a-string-without-cut-in-the-middle-of-a-word-in-rails – dp7 Mar 16 '16 at 19:50

1 Answers1

0

As per documentation:

truncate("Once upon a time in a world far far away", length: 17)
# => "Once upon a ti"

truncate("Once upon a time in a world far far away", length: 17, separator: ' ')
# => "Once upon a"

Please find more on this in the following SO link:

Truncate a string without cut in the middle of a word in rails

Community
  • 1
  • 1
dp7
  • 6,651
  • 1
  • 18
  • 37