15

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(' ', '')
1
  • 1
    Not sure if that was intentional, but there shouldn't be a period after gsub. "gsub(' ', '')" not "gsub.(' ', '')" Commented Jun 16, 2009 at 12:42

9 Answers 9

34

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/,'')
Sign up to request clarification or add additional context in comments.

Comments

15
>> "5 900 00".gsub(' ','')
=> "590000"

Is it really a string?

.gsub returns the value, if you want to change the variable try .gsub!(" ", "")

Comments

6

Depending on the situation, you may not need a regular expression. String#tr should work well if you want to replace a single space with the empty string:

telemachus ~ $ irb
>> "500 500 12".tr(' ', '')
=> "50050012"
>> "500 500 12".tr!(' ', '')
=> "50050012"

As with gsub and gsub!, the ! method makes the change in place as opposed to returning the changed result. I don't know which you want here.

In a case like this, tr seems more straightforward to me. I'm not looking for optimization, but it is good to remember that there are lots of string methods other than regular expressions.

3 Comments

Not sure if tr is a good option? Translates str in place, using the same rules as String#tr. Returns str, or nil if no changes were made. So if the string doesn't match anything, it will return nil.
@23inhouse I'm not sure what you mean. For the first tr, I think you mean tr!. (The whole difference between tr and tr! is that tr does not make the changes in place. As for returning nil, whether or not that's ok depends on the larger application. The op hasn't given us enough to go on. It might be fine or it might be a mistake.
true, i thought it worth mentioning.
2

I suggest doing str.gsub!(/\s+/, '') for efficiency reasons.

Comments

2

Try this hope this will helpful :

2.2.1 :001> str= "  Jai   Kumar rajput ";
  # "  Jai   Kumar rajput "
2.2.1 :001> str.squish.downcase.tr(" ","");
  # "JaiKumarRajput" 

Comments

1

"5 900 000".gsub(/\s/,'') works fine

From what I see you wrote gsub dot (foo,bar) where it must be string.gsub(foo,bar)

Comments

0
print "5 900 000".gsub(/\s/, '')

Works for me.

Are you affecting the result to the variable ?

Comments

0

do you mean

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

with the exclamation mark?

Comments

0

The funny thing is that when I print the string i get

697\302\240000

but what gets to the database is: 697 000. I know that patterns i have given should work as well as your suggestions, but this seems to be a little bit 'dodgy' case :-)

2 Comments

in this case try str.gsub!(/\D/,'')
That explains it. \302\240 is the octal representation of hexadecimal 0xC2 0xA0. The latter is how UTF-8 represents U+00A0. Lastly U+00A0 is a non-breaking space. en.wikipedia.org/wiki/Non-breaking_space

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.