# Splitting a paragraph into words and spaces

**URL:** <https://www.ruby-forum.com/t/splitting-a-paragraph-into-words-and-spaces/97126>\
**Category:** Rails\
**Created:** [July 5, 2006, 4:36am UTC](https://www.ruby-forum.com/t/splitting-a-paragraph-into-words-and-spaces/97126 "2006-07-05T04:36:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [July 5, 2006, 4:36am UTC](https://www.ruby-forum.com/t/splitting-a-paragraph-into-words-and-spaces/97126/1 "2006-07-05T04:36:05Z")

</div>

I’m using this:

\<% words = article.content.split(/ /) %\>  
\<%= words[0…20] %\>

to (ostensibly) split a paragraph into component words, with spaces in  
between, then print to html only the first 20 items, words and spaces. I  
got this (split(/ /)) from the online pickaxe book at  
[http://rubycentral.com/book/ref\_c\_string.html#String.split](http://rubycentral.com/book/ref_c_string.html#String.split) . The problem  
is, the resulting array seems to contain no spaces, only words, and  
what’s being printed to html is onehugewordthatlookslikethis.

Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [July 5, 2006, 4:53am UTC](https://www.ruby-forum.com/t/splitting-a-paragraph-into-words-and-spaces/97126/2 "2006-07-05T04:53:00Z")

</div>

try this…

\<% words = article.content.split(/ /) %\>  
\<%= words[0…20].join ’ ’ %\>

This will join the first 20 elements with spaces in between. When you  
split,  
it removes the text which you split on.

mark

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [July 5, 2006, 6:11am UTC](https://www.ruby-forum.com/t/splitting-a-paragraph-into-words-and-spaces/97126/3 "2006-07-05T06:11:45Z")

</div>

You are THE MAN!!!  
Works perfectly. Thanks.

Mark Van H. wrote:

> try this…
> 
> \<% words = article.content.split(/ /) %\>  
> \<%= words[0…20].join ’ ’ %\>
> 
> This will join the first 20 elements with spaces in between. When you  
> split,  
> it removes the text which you split on.
> 
> mark
