666

What is the Ruby function to remove all white spaces? I'm looking for something kind of like PHP's trim()?

5
  • 38
    Your question is not clear: Do you want to remove all whitespace or do you want to get rid of leading and trailing whitespace? Oct 28, 2009 at 2:00
  • 29
    PHP's trim() strips whitespace "from the beginning and end of a string" (as stated within documentation), it does not remove "all whitespaces".
    – Tadeck
    Jan 11, 2012 at 8:36
  • 3
    When in doubt, look at the Ruby online documentation for the String class (see .strip below).
    – Merovex
    Mar 3, 2013 at 14:24
  • 3
    Note that all answers using String#strip or matching /\s+/ will remove only ASCII whitespace. If you want to ensure any non-ASCII whitespace is captured too (e.g. HTML's &nbsp) see the oddly unpopular answer from @EBooker.
    – MatzFan
    Nov 25, 2017 at 20:43
  • 1
    Pity that such great answers cannot get the final dignity of one being accepted Jul 17, 2019 at 14:31

26 Answers 26

929

If you want to remove only leading and trailing whitespace (like PHP's trim) you can use .strip, but if you want to remove all whitespace, you can use .gsub(/\s+/, "") instead .

7
  • 6
    Does "/\s+/" simple mean whitespace? Jul 13, 2011 at 8:50
  • 61
    \s+ means 1 or more whitespace characters (space, newline, tab). The // surrounding show that it's a regular expression.
    – dylanfm
    Jul 27, 2011 at 12:26
  • 4
    also note that ActiveSupport (rails) adds String#remove(pattern) which is a shortcut to String#gsub(pattern, '') (plus a destructive remove! version). Quite handy.
    – m_x
    Jul 28, 2014 at 12:58
  • 1
    Technically, \s means " ", "\n", "\t", "\r", "\f" or"\v". However, only the first 3 (or possibly 4) are even remotely common.
    – Tom Lord
    Jul 15, 2016 at 14:15
  • 2
    @LouisSayers Technically true. My preference is to communicate intent; "replace all runs of whitespace with empty string" seems more direct to me (and possibly an insignificant bit less string manipulation) than "replace each whitespace character with empty string". YMMV.
    – joel.neely
    Jun 17, 2017 at 16:18
564
s = "I have white space".delete(' ')

And to emulate PHP's trim() function:

s = "   I have leading and trailing white space   ".strip
12
  • 14
    this is much more readable than the regex, why is it not as popular?
    – ckarbass
    Aug 17, 2012 at 3:27
  • 102
    @ckarbass: Because many people prefer overly complex solutions to simple problems. It goes away with experience. Aug 17, 2012 at 4:40
  • 120
    @ckarbass @Ed S. It isn't as popular because it isn't the same. The original question used the phrase "all whitespace", which includes tabs, newlines, etc. This proposed answer will not remove those other whitespace characters. As for "overly complex", I suggest comparing the simple regular expression to .delete(' ').delete('\t').delete('\n') ..., which is overly verbose and provides many opportunities for typos and errors of omission.
    – joel.neely
    Sep 8, 2012 at 23:48
  • 18
    @joel.neely: I answered this question a long time ago, but read the question again, this time more carefully. The OP asked for "a function to remove all whitespace", but then asked for "something like PHP's trim()". So, it's a bit difficult to know exactly what they want here. trim() certainly does not remove newlines and other whitespace characters. You're choosing one interpretation of a vague question. Oct 4, 2012 at 17:51
  • 4
    @joel.neely: That said, I agree that a solution which goes beyond the literal interpretation of the question is a better one in this case (i.e., a regex removing all characters which would constitute whitespace rather than a string of delete() calls.) Oct 4, 2012 at 21:49
199

String#strip - remove all whitespace from the start and the end.

String#lstrip - just from the start.

String#rstrip - just from the end.

String#chomp (with no arguments) - deletes line separators (\n or \r\n) from the end.

String#chop - deletes the last character.

String#delete - x.delete(" \t\r\n") - deletes all listed whitespace.

String#gsub - x.gsub(/[[:space:]]/, '') - removes all whitespace, including unicode ones.


Note: All the methods above return a new string instead of mutating the original. If you want to change the string in place, call the corresponding method with ! at the end.

8
  • The String#delete example appears to use a regex, but \s is in quotes instead of slashes. Also I couldn't find any mention in the documentation that delete can take a regex as an argument.
    – slothbear
    Feb 5, 2017 at 15:31
  • @slothbear, it's not a regex, it's a small set of patterns that resemble regexes. As for the documentation #delete is said to work similarly to #count. You can try it in the console as well.
    – ndnenkov
    Feb 5, 2017 at 15:36
  • Thanks for teaching me something new. And also thanks for the reminder to try things in the smallest possible context (command line).
    – slothbear
    Feb 8, 2017 at 5:21
  • 1
    @juliangonzalez, #delete doesn't, #delete! does.
    – ndnenkov
    Jun 11, 2017 at 5:55
  • 6
    Only the final example in this answer catches the dread ASCII 160 'non-breaking space', the bane of web scrapers. #strip does not. See stackoverflow.com/questions/4859438/…
    – MatzFan
    Apr 15, 2019 at 18:19
180

Related answer:

"   clean up my edges    ".strip

returns

"clean up my edges"
4
  • That's the one I forgot about. I knew there was a method to remove whitespace which would do so by default if no arguments were passed. +1 Oct 28, 2009 at 1:56
  • This is equivalent to trim. Please refer to the quote from @Tadeck above.
    – Brett H
    Jul 12, 2012 at 2:48
  • 3
    If there is a possibility that the variable is nil, be sure to run .to_s method before running strip so that the strip method does not raise an error. Ex. str=nil; str.to_s.strip #=> ""
    – scarver2
    Oct 26, 2012 at 20:15
  • I prefer some_data.strip! if some_data.is_a? String Jul 30, 2018 at 14:12
142

If you are using Rails/ActiveSupport, you can use squish method. It removes white space on both ends of the string and groups multiple white space to single space.

For eg.

" a  b  c ".squish

will result to:

"a b c"

Check this reference from api.rubyonrails.org.

4
  • 4
    Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference.
    – kleopatra
    Oct 17, 2013 at 10:59
  • 2
    I think this answer was enough explained and the fact that link was reference since the answer itself was clear explained. This function was good, thanks
    – ksugiarto
    Nov 4, 2013 at 5:43
  • 4
    This is from ActiveSupport. You don't need all of Rails to use it, but you do need at least ActiveSupport and a require 'active_support/core_ext/string/filters' Nov 26, 2014 at 19:45
  • 2
    To be clear, this is any whitespace. E.g. "a \t \n \f \r \v b".squish == "a b" Feb 25, 2020 at 23:47
106
"1232 23 2 23 232 232".delete(' ')
=> "123223223232232"

Delete works faster =)

user         system     total      real
gsub, s      0.180000   0.010000   0.190000 (0.193014)
gsub, s+     0.200000   0.000000   0.200000 (0.196408)
gsub, space  0.220000   0.000000   0.220000 (0.222711)
gsub, join   0.200000   0.000000   0.200000 (0.193478)
delete       0.040000   0.000000   0.040000 (0.045157)
3
50

It's a bit late, but anyone else googling this page might be interested in this version -

If you want to clean up a chunk of pre-formatted text that a user may have cut & pasted into your app somehow, but preserve the word spacing, try this:

content = "      a big nasty          chunk of     something

that's been pasted                        from a webpage       or something        and looks 

like      this

"

content.gsub(/\s+/, " ").strip

#=> "a big nasty chunk of something that's been pasted from a webpage or something and looks like this"
2
50

Ruby's .strip method performs the PHP equivalent to trim().

To remove all whitespace:

"  leading    trailing   ".squeeze(' ').strip
=> "leading trailing"

@Tass made me aware that my original answer removes duplicate letters in succession - YUCK! I've since switched to the squish method which is smarter about such occurrences if using the Rails framework.

require 'active_support/all'
"  leading    trailing   ".squish
=> "leading trailing"

"  good    men   ".squish
=> "good men"

Cite: http://apidock.com/rails/String/squish

1
  • 1
    This will remove "joined" duplicate characters. "good men".squeeze.strip will return "god men"
    – Tass
    Jul 5, 2013 at 18:07
31

To remove whitespace on both sides:

Kind of like php's trim()

"   Hello  ".strip

To remove all spaces:

"   He    llo  ".gsub(/ /, "")

To remove all whitespace:

"   He\tllo  ".gsub(/\s/, "")
27
" Raheem Shaik ".strip

It will removes left & right side spaces. This code would give us: "Raheem Shaik"

24

split.join will blast all spaces anywhere in the string.

"  a b  c    \nd  \t   ".split.join
> "abcd"

It's easy to type and remember, so it's nice on the console and for quick hacking. Arguably not welcome in serious code though as it masks the intent.

(Based on Piotr's comment in Justicle's answer above.)

1
  • 1
    Many, many thanks for this comment :-) This is the only method that works if you have long string which looks like a paragraph.
    – Boomerange
    Jan 19, 2017 at 21:20
20

Also don't forget:

$ s = "   I have white space   ".split
=> ["I", "have", "white", "space"]
2
  • 6
    So s.split.join will do the job. Jun 14, 2012 at 7:59
  • 1
    This is nice when iterating: [" Hello World", "Big Giraffe "].map(&:split).map(&:join) #=> ["HelloWorld", "BigGiraffe"]
    – tbloncar
    Mar 8, 2014 at 19:27
20

To remove all whitespace characters, including non-ASCII ones (e.g., non-breaking space), in a string you could try using gsub with :space as the matcher and "" (empty string) as the replacement:

string = "   Some Special Text Values ".gsub(/[[:space:]]+/, "")
puts '[' + string + ']' # "[SomeSpecialTextValues]

string = "   em spaces   ".gsub(/[[:space:]]+/, "")
puts '[' + string.strip + ']' # [emspaces]

To remove leading and trailing whitespace characters, you can try:

string = "   em spaces   "
 .gsub(/^[[:space:]]+/, "")
 .gsub(/[[:space:]]+$/, "")
puts '[' + string.strip + ']' # [em spaces]

string = "   em spaces   ".strip
puts '[' + string.strip + ']' # [   em spaces]

Note how strip only removes the trailing whitespaces (i.e., SPACE U+0020) while leaving the leading whitespaces (i.e., EM SPACE U+2003) as-is. That's because strip only removes ASCII whitespace characters.

2
  • 3
    This is actually the best answer IMHO, as in the wild HTML &nbsp and any other non-ASCII whitespaces will not be removed by String#strip or matched by /\s/. See the section entitled "POSIX bracket expressions" in the Regexp docs
    – MatzFan
    Nov 25, 2017 at 20:31
  • Instead of creating another similar answer, I decided to piggyback on yours, EBooker. Great job!
    – uzluisf
    Jul 17, 2023 at 21:09
10

Use gsub or delete. The difference is gsub could remove tabs, while delete cannot. Sometimes you do have tabs in files which are added by the editors.

a = "\tI have some whitespaces.\t"
a.gsub!(/\s/, '')  #=>  "Ihavesomewhitespaces."
a.gsub!(/ /, '')   #=>  "\tIhavesomewhitespaces.\t"
a.delete!(" ")     #=>  "\tIhavesomewhitespaces.\t"
a.delete!("/\s/")  #=>  "\tIhavesomewhitespaces.\t"
a.delete!('/\s/')  #=>  using single quote is unexpected, and you'll get "\tI have ome whitepace.\t"
7

The gsub method will do just fine.
The gsub method can be called on a string and says:

a = "this is a string"
a = a.gsub(" ","")
puts a
#Output: thisisastring

The gsub method searches for every occurrence of the first argument and replaces it with the second argument. In this case, it will replace every space within the string and remove it.

Another example:

b = "the white fox has a torn tail"

Let's replace every occurrence of the letter " t " with a capital " T "

b = b.gsub("t","T")
puts b 
#Output: The whiTe fox has a Torn Tail
7

A lot of suggestions work here, but when I read your question and the specific line saying "removing all the whitespace", what came to my mind was this:

" a b c " => "abc"

And if that is really what is required, you can do this simple operation

wide_string = " a b c "

narrow_string = wide_string.delete(" ")

# you can pass all the different kinds 
# of whitespaces that you want to remove

puts narrow_string # => "abc"
1
  • Love it. Clean and intuitive. Should be performant as well, I would think. Feb 21 at 14:06
6
"asd sda sda sd".gsub(' ', '')
=> "asdsdasdasd"
1
  • but this removes only spaces, not all white spaces
    – Gavriel
    Feb 11, 2016 at 6:49
5

For behavior exactly matching PHP trim, the simplest method is to use the String#strip method, like so:

string = "  Many have tried; many have failed!    "
puts "Original [#{string}]:#{string.length}"
new_string = string.strip
puts "Updated  [#{new_string}]:#{new_string.length}"

Ruby also has an edit-in-place version, as well, called String.strip! (note the trailing '!'). This doesn't require creating a copy of the string, and can be significantly faster for some uses:

string = "  Many have tried; many have failed!    "
puts "Original [#{string}]:#{string.length}"
string.strip!
puts "Updated  [#{string}]:#{string.length}"

Both versions produce this output:

Original [  Many have tried; many have failed!    ]:40
Updated  [Many have tried; many have failed!]:34

I created a benchmark to test the performance of some basic uses of strip and strip!, as well as some alternatives. The test is this:

require 'benchmark'

string = 'asdfghjkl'
Times = 25_000

a = Times.times.map {|n| spaces = ' ' * (1+n/4); "#{spaces}#{spaces}#{string}#{spaces}" }
b = Times.times.map {|n| spaces = ' ' * (1+n/4); "#{spaces}#{spaces}#{string}#{spaces}" }
c = Times.times.map {|n| spaces = ' ' * (1+n/4); "#{spaces}#{spaces}#{string}#{spaces}" }
d = Times.times.map {|n| spaces = ' ' * (1+n/4); "#{spaces}#{spaces}#{string}#{spaces}" }

puts RUBY_DESCRIPTION
puts "============================================================"
puts "Running tests for trimming strings"

Benchmark.bm(20) do |x|
  x.report("s.strip:")                 { a.each {|s| s = s.strip } }
  x.report("s.rstrip.lstrip:")         { a.each {|s| s = s.rstrip.lstrip } }
  x.report("s.gsub:")                  { a.each {|s| s = s.gsub(/^\s+|\s+$/, "") } }
  x.report("s.sub.sub:")               { a.each {|s| s = s.sub(/^\s+/, "").sub(/\s+$/, "") } }

  x.report("s.strip!")                 { a.each {|s| s.strip! } }
  x.report("s.rstrip!.lstrip!:")       { b.each {|s| s.rstrip! ; s.lstrip! } }
  x.report("s.gsub!:")                 { c.each {|s| s.gsub!(/^\s+|\s+$/, "") } }
  x.report("s.sub!.sub!:")             { d.each {|s| s.sub!(/^\s+/, "") ; s.sub!(/\s+$/, "") } }
end

These are the results:

ruby 2.2.5p319 (2016-04-26 revision 54774) [x86_64-darwin14]
============================================================
Running tests for trimming strings
                           user     system      total        real
s.strip:               2.690000   0.320000   3.010000 (  4.048079)
s.rstrip.lstrip:       2.790000   0.060000   2.850000 (  3.110281)
s.gsub:               13.060000   5.800000  18.860000 ( 19.264533)
s.sub.sub:             9.880000   4.910000  14.790000 ( 14.945006)
s.strip!               2.750000   0.080000   2.830000 (  2.960402)
s.rstrip!.lstrip!:     2.670000   0.320000   2.990000 (  3.221094)
s.gsub!:              13.410000   6.490000  19.900000 ( 20.392547)
s.sub!.sub!:          10.260000   5.680000  15.940000 ( 16.411131)
3

My personal preference is using the method .tr

as in:

string = "this is a string to smash together"

string.tr(' ', '') # => "thisisastringtosmashtogether"

Thanks to @FrankScmitt for pointing out that to make this delete all whitespace(not just spaces) you would need to write it as such:

string = "this is a string with tabs\t and a \nnewline"

string.tr(" \n\t", '') # => "thisisastringwithtabsandanewline"
4
  • but this removes only spaces, not all white spaces
    – Gavriel
    Feb 10, 2016 at 7:27
  • To remove all white spaces (space, tab, newline), consider using s.tr(" \t\n", '') instead. Feb 10, 2016 at 7:33
  • @Gavriel - I misread/misunderstood the question, thank you for pointing that out. Feb 11, 2016 at 6:44
  • @FrankSchmitt I added your correction to my answer, to more properly answer the OP's question. Thank you for correcting me. Feb 11, 2016 at 6:44
3

I was trying to do this as I wanted to use a records "title" as an id in the view but the titles had spaces.

a solution is:

record.value.delete(' ') # Foo Bar -> FooBar
3

I'm a bit late to the game, but I remove trailing and leading whitespaces by using strip!. If you have an array, such as I did, I needed to iterate through the array and save it after the instance ended. The ! took care of this. This removed all whitespaces at the end or the beginning, not just the first leading or the last trailing.

For example:

array = ["hello ","   Melanie", "is", " new ", "to  ", " programming"]
array.each do |i|
  i.strip!
end

This would output to: ["hello","Melanie", "is", "new ", "to", "programming"]. I further explored/shared this in a video I made to highlight this code for similar question I had.

I'm newer to programming and using strip did not work as it didn't save it to the array after the loop ended.

2

I would use something like this to remove all spaces:

my_string = "   Foo  \t bar\nbaz quux \n   "

my_string.split.join
=> "Foobarbazquux"
1
1

Ruby's .scan() and .join() methods of String can also help to overcome whitespace in string.

scan(/\w+/).join will remove all spaces and join the string

string = "White spaces in me".scan(/\w+/).join
=>"Whitespacesinme"

It is also removing space from left and right part of the string. Means ltrim, rtrim and trim. Just in case if someone has background over C, FoxPro or Visual Basic and jump in Ruby.

2.1.6 :002 > string = " White spaces in me ".scan(/\w+/).join => "Whitespacesinme" 2.1.6 :003 > string = " White spaces in me".scan(/\w+/).join => "Whitespacesinme" 2.1.6 :004 > string = "White spaces in me ".scan(/\w+/).join => "Whitespacesinme" 2.1.6 :005 >

1
  • 1
    @AmitPandya Thank you so much for pointing out additional key points of .scan() method. Appreciated !!! Apr 13, 2016 at 18:03
0

You can try this:

"ab c d efg hi ".split.map(&:strip)

in order to get this:

["ab, "c", "d", "efg", "hi"]

or if you want a single string, just use:

"ab c d efg hi ".split.join
0

If you want to remove duplicate spaces and keep single spaces intact, use:

"   Hello     World!   ".sqeeze(" ").strip

This is way faster than split(" ").join and other alternatives.

0

If I am on Rails I use squish to remove all extra spaces in a string, in Ruby alone I would use strip to remove trailing spaces from beginning an d end of the string and finally if I want the same functionality as squish without having to import ActiveModel, I use this mystring.gsub(/\s{1,}/, ' ').strip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.