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"
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"
< p > <代码>
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 >
< /代码> < / p >
string = " Many have tried; many have failed! "
puts "Original [#{string}]:#{string.length}"
new_string = string.strip
puts "Updated [#{new_string}]:#{new_string.length}"
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"