简体   繁体   English

Ruby 1.8中所有空格的gsub字符串

[英]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? 如何在我的模式中添加所有空格代码(例如3000,2060,205f)?

In Ruby 1.9 I just added \  and other codes, but how do it in 1.8? 在Ruby 1.9中,我刚刚添加了\ 和其他代码,但是如何在1.8中添加?

I think i found answer. 我想我找到了答案。 In ActiveSupport::Multibyte::Chars is a UNOCODE_WHITESPACE constant. 在ActiveSupport :: Multibyte :: Chars中是UNOCODE_WHITESPACE常量。 Solution: 解:

pattern = ActiveSupport::Multibyte::Chars::UNICODE_WHITESPACE.collect do |c|
  c.pack "U*"
end.join '|'
puts "qwe rty uiop".mb_chars.gsub(/#{pattern}/,'')
#=> qwertyuiop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM