How many times have you had a string that has some extra whitespace in the start/mid/end of a string. To remove them you would have to use strip
that would successfully remove the whitespace at the end of a string. Let's see
" Hello, World ".strip
=> "Hello, World"
It has successfully removed the whitespaces at the start and end. But what about the middle ones?. Usually you would have to write a regex for that
" Hello, World ".strip.gsub(/[[:space:]]+/, " ")
=> "Hello, World"
When using Rails. This method is available as String#squish
which does the same thing!
"Hello, World ".squish
=> "Hello, World"
Resources
Top comments (0)