Code Prostitute

the sordid details of my career as a code prostitute

Ruby / Rails: Remove Extra Space

leave a comment »

…or anything else you want from a string.


" myemail @ address. co m ".gsub(/\s/, '')

gsub() substitutes all instances found (g = global), if you just want to substitute the first instance use sub()

Or, if you want to trim the whitespace from each end:


result = " myemail@address.com ".strip

To just trim the whitespace on the left or the right, use these (respectively):
lstrip()
rstrip()


Written by codeprostitute

May 1, 2009 at 12:16 pm

Leave a Reply