Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gsub string for all spaces in Ruby 1.8

I have a string with spaces (one simple space and one ideographic space):

"qwe rty uiop".gsub(/[\s]+/,'')    #=> "qwe rtyuiop"

How can I add all space-codes (for example 3000, 2060, 205f) in my pattern?

In Ruby 1.9 I just added \u3000 and other codes, but how do it in 1.8?

like image 580
avy Avatar asked Dec 22 '25 14:12

avy


1 Answers

I think i found answer. In ActiveSupport::Multibyte::Chars is a UNOCODE_WHITESPACE constant. Solution:

pattern = ActiveSupport::Multibyte::Chars::UNICODE_WHITESPACE.collect do |c|
  c.pack "U*"
end.join '|'
puts "qwe rty uiop".mb_chars.gsub(/#{pattern}/,'')
#=> qwertyuiop
like image 140
avy Avatar answered Dec 24 '25 05:12

avy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!