How to remove space between 2 numbers ?
I have a String "1 026 personnes aiment ça" and I would like to get only 1026 (as Integer)
My attempt:
text = "1 026 personnes aiment ça"
# => "1 026 personnes aiment ça"
z = text.split(' ').first
# => "1 026"
z.to_i
# => 1
z.strip
# => "1 026"
z.class
# => String
The #gsub method can replace all non-digit characters in the String and then you can transform it into an Integer with #to_i:
"1 026 personnes aiment ça".gsub(/\D/, "").to_i
#~> 1026
you can try this it`s work for me
(?<=\d) +(?=\d+(?:\s|$))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With