# How to trim a string?

**URL:** <https://www.ruby-forum.com/t/how-to-trim-a-string/58983>\
**Category:** Rails\
**Created:** [April 27, 2006, 4:20pm UTC](https://www.ruby-forum.com/t/how-to-trim-a-string/58983 "2006-04-27T16:20:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 27, 2006, 4:20pm UTC](https://www.ruby-forum.com/t/how-to-trim-a-string/58983/1 "2006-04-27T16:20:53Z")

</div>

This must be really simple but I have looked everywhere.

I have a \<%= item.description -% in my view and I want to trim it to 10  
words.

The description can be long and I want to trim this in my preview?

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 27, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/how-to-trim-a-string/58983/2 "2006-04-27T16:33:17Z")

</div>

There may be a better way, but “dassdfasdfs”[0…10]

mark

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 27, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/how-to-trim-a-string/58983/3 "2006-04-27T16:33:55Z")

</div>

“Rails is a full-stack framework for developing database-backed web  
applications according to the Model-View-Control pattern. From the  
Ajax in the view, to the request and response in the controller, to  
the domain model wrapping the database, Rails gives you a pure-Ruby  
development environment. To go live, all you need to add is a database  
and a web server.”.split[1…10].join(" ")

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 27, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/how-to-trim-a-string/58983/4 "2006-04-27T16:37:50Z")

</div>

Mathieu C. wrote:

> “Rails is a full-stack framework for developing database-backed web  
> applications according to the Model-View-Control pattern. From the  
> Ajax in the view, to the request and response in the controller, to  
> the domain model wrapping the database, Rails gives you a pure-Ruby  
> development environment. To go live, all you need to add is a database  
> and a web server.”.split[1…10].join(" ")

Thanks, I knew it would be simple! Brain hurts!

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 27, 2006, 4:40pm UTC](https://www.ruby-forum.com/t/how-to-trim-a-string/58983/5 "2006-04-27T16:40:49Z")

</div>

> and a web server.".split[1…10].join(" ")

Oups : [0…9]

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 27, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/how-to-trim-a-string/58983/6 "2006-04-27T16:37:57Z")

</div>

On 4/27/06, James W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This must be really simple but I have looked everywhere.
> 
> I have a \<%= item.description -% in my view and I want to trim it to 10  
> words.
> 
> The description can be long and I want to trim this in my preview?

item.description.split[0…9].join " "

It blows away whitespace like extra spaces and carriage returns, but  
that wouldn’t have mattered in a view anyway so it should be fine.

– James

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 27, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/how-to-trim-a-string/58983/7 "2006-04-27T17:04:59Z")

</div>

Or [0…10]
