# String cutter

**URL:** <https://www.ruby-forum.com/t/string-cutter/170107>\
**Category:** Rails\
**Created:** [June 12, 2009, 12:11pm UTC](https://www.ruby-forum.com/t/string-cutter/170107 "2009-06-12T12:11:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [June 12, 2009, 12:11pm UTC](https://www.ruby-forum.com/t/string-cutter/170107/1 "2009-06-12T12:11:58Z")

</div>

Hey,

I am wondering if there is some kind of built in function to strip the  
text to given length, but I dont want to cut the word in the middle. 🙂

I know that this works, but it cuts the text.  
\<%= @post.body[0…60] %\>

Is there a way to do the same thing but keep the last word as it is or  
if the length of the word is longer than given size, strip the whole  
word, not just the part of it.

Thank you.  
P.

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [June 12, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/string-cutter/170107/2 "2009-06-12T15:35:12Z")

</div>

On 12 Jun 2009, at 12:11, Petan C. wrote:

> I am wondering if there is some kind of built in function to strip the  
> text to given length, but I dont want to cut the word in the  
> middle. 🙂
> 
> I know that this works, but it cuts the text.  
> \<%= @post.body[0…60] %\>
> 
> Is there a way to do the same thing but keep the last word as it is or  
> if the length of the word is longer than given size, strip the whole  
> word, not just the part of it.

> **[Awesome Truncation in Rails](https://tumble.mlcastle.net/post/17704444326/awesome-truncation-in-rails)**
>
> For a project I’m working on, we’d like to have the rails truncate helper truncate strings at word boundaries rather than at the in the middle of a word after exactly a certain number of...

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [June 12, 2009, 10:36pm UTC](https://www.ruby-forum.com/t/string-cutter/170107/3 "2009-06-12T22:36:39Z")

</div>

Thank you,  
that is exactly what I was looking for.

Cheeers.  
P.

> [Awesome Truncation in Rails - mlcastle](http://vermicel.li/blog/2009/01/30/awesome-truncation-in-rails.html)
> 
> Best regards
> 
> Peter De Berdt
