Strip/replace spaces within a string

regex, ruby

Solution

If you want to do the replacement in place, you need to use:

str.gsub!(/\s/,'')

Alternatively, gsub returns the string with the replacements

str2 = str.gsub(/\s/,'')

EDIT: Based on your answer, it looks like you have some unprintable characters embedded in the string, not spaces. Using /\D/ as the search string may be what you want. The following will match any non-digit character and replace it with the empty string.

str.gsub!(/\D/,'')

Problem

Given a string `"5 900 000"` I want to get rid of the spaces using `gsub` with the following pattern: ``` gsub(/\s/, '') ``` but that doesn't seem to work. Nor does: ``` gsub(' ', '') ```

Original source