# Removing whitespaces from string

**URL:** <https://www.ruby-forum.com/t/removing-whitespaces-from-string/145150>\
**Category:** Rails\
**Created:** [August 6, 2008, 3:00pm UTC](https://www.ruby-forum.com/t/removing-whitespaces-from-string/145150 "2008-08-06T15:00:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [August 6, 2008, 3:00pm UTC](https://www.ruby-forum.com/t/removing-whitespaces-from-string/145150/1 "2008-08-06T15:00:29Z")

</div>

Hi  
How can I remove all whitespaces from a string. even whitespace inside  
the string.

Thanks

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [August 6, 2008, 3:22pm UTC](https://www.ruby-forum.com/t/removing-whitespaces-from-string/145150/2 "2008-08-06T15:22:27Z")

</div>

If you want to eliminate all spaces, you can use String#gsub.

" a b c “.gsub(” ", “”) #=\> “abc”

However, if you want to eliminate leading and trailing spaces and  
collapse  
several internal spaces to one, you can combine String#squeeze and  
String#strip.

" a b c ".squeeze.strip #=\> “a b c”

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [August 7, 2008, 3:19am UTC](https://www.ruby-forum.com/t/removing-whitespaces-from-string/145150/3 "2008-08-07T03:19:07Z")

</div>

Thanks of the tip on #squeeze Craig! I didn’t know about that method.
