# Strip White spaces

**URL:** <https://www.ruby-forum.com/t/strip-white-spaces/115377>\
**Category:** Rails\
**Created:** [September 24, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/strip-white-spaces/115377 "2007-09-24T16:54:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 24, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/strip-white-spaces/115377/1 "2007-09-24T16:54:36Z")

</div>

Hello All,

I am trying to strip white spaces out of a html file or a very long  
string and am coming up a blank. Does anyone have some suggestions as  
to how to tackle this?

Regards,

j

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 24, 2007, 8:53pm UTC](https://www.ruby-forum.com/t/strip-white-spaces/115377/2 "2007-09-24T20:53:16Z")

</div>

I am sure there is a more elegant solution but I think the following  
could work:

your\_string.squeeze(" “).split(” ").join

On Sep 24, 9:53 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 24, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/strip-white-spaces/115377/3 "2007-09-24T23:12:34Z")

</div>

what about  
yourstring.gsub!(/\s+/,’’)

almes wrote:

> > Hello All,
> 
> >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 25, 2007, 12:48pm UTC](https://www.ruby-forum.com/t/strip-white-spaces/115377/4 "2007-09-25T12:48:53Z")

</div>

The problem with this approach is that it removes all spaces form the  
document, which is not my intention. I wish to remove all white  
spaces from the document while retaining all spaces between words.  
Does anybody know how to do this?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 25, 2007, 12:54pm UTC](https://www.ruby-forum.com/t/strip-white-spaces/115377/5 "2007-09-25T12:54:55Z")

</div>

My mistake. this works great.

Thank you for your help.

On Sep 25, 10:47 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 25, 2007, 12:45pm UTC](https://www.ruby-forum.com/t/strip-white-spaces/115377/6 "2007-09-25T12:45:51Z")

</div>

Thanks for your reply but I can not use split because there is HTML  
mark-up in the document that I wish to parse.
