如何从Ruby中的字符串中删除空格

时间:2013-12-06 18:54:47

标签: ruby string

鉴于此片段:

require 'date'

raw_data = ["Mon\n                              9:00 -   1:00pm,   2:00 -   6:00pm",
 "Tue\n                            10:00 -   1:00pm,   2:00 -   7:30pm",
 "Wed\n                              9:00 -   1:00pm,   2:00 -   6:00pm",
 "Thu\n                              9:00 -   1:00pm,   2:00 -   6:00pm",
 "Fri\n                              9:00 -   1:00pm,   2:00 -   6:00pm",
 "Sat",
 "Sun"]

 raw_data.each do |element|
  a =  element.gsub(/\n/,',').gsub(/\s+/, '').split(',')

  day = a[0].to_s.downcase!
   "day: #{day}"

  map = { "mon" => "monday","tue" => "tuesday", "wed" => "wednesday", "thu" => "thursday", "fri" => "friday", "sat" => "saturday", "sun" => "sanday" }
  map.each {|k,v| day.gsub!(k,v) }

  day_from = a[1]
  day_from = day_from.to_s.split('-')[0]
  day_to = a[2]
  day_to = day_to.to_s.split('-')[1]
  day_to = day_to.gsub(/pm/,'')  unless day_to.nil?

  # not working??
  day_to = day_to.gsub(/\s+/,'') unless day_to.nil?
  p day_to
 end

如何获得此输出:

# desired
# "6:00"
# "7:30"
# "6:00"
# "6:00"
# "6:00"
# nil
# nil

现在这段代码返回:

#
# "  6:00"
# "  7:30"
# "  6:00"
# "  6:00"
# "  6:00"
# nil
# nil
谷歌说要使用:

  # not working??
  day_to = day_to.gsub(/\s+/,'') unless day_to.nil?

但是这个解决方案不知何故......

更新

代码hex code和新输入值

require 'date'
require 'hex_string'

raw_data = ["\n                            Mon\n                              8:30 -   6:00pm\n                          ",
 "\n                            Tue\n                              8:30 -   6:00pm\n                          ",
 "\n                            Wed\n                              8:30 -   6:00pm\n                          ",
 "\n                            Thu\n                              8:30 -   6:00pm\n                          ",
 "\n                            Fri\n                              8:30 -   6:00pm\n                          ",
 "\n                            Sat\n                              8:30 -   4:00pm\n                          ",
 "\n                            Sun\n                            10:00 -   3:00pm\n                          "

]

puts raw_data.size

 raw_data.each do |element|
  a =  element.gsub(/\n/,',').gsub(/\s+/, '').split(',')

  day = a[1].to_s.downcase!
  #puts  "day: #{day}"

  map = { "mon" => "monday","tue" => "tuesday", "wed" => "wednesday", "thu" => "thursday", "fri" => "friday", "sat" => "saturday", "sun" => "sanday" }
  map.each {|k,v| day.gsub!(k,v) }

  day_from = a[2]
  day_from = day_from.to_s.split('-')[0]
  day_from.to_s.strip unless day_from.nil?
  p "day regular: #{day_from}"
  p "day from(hex): #{day_from.to_hex_string}"
  p "-----------------------------------------"

  day_to = a[2].to_s.split('-')[1]
  day_to = day_to.gsub(/pm/,'')  unless day_to.nil?

  # not working
  day_to = day_to.gsub(/\s+/,'') unless day_to.nil?
  p "day regular: #{day_to}"
  p "day_to(hex): #{day_to.to_hex_string}"
  p "-----------------------------------------"
 end



"day regular:   8:30"
"day from(hex): c2 a0 c2 a0 38 3a 33 30"
"-----------------------------------------"
"day regular:   6:00"
"day_to(hex): c2 a0 c2 a0 36 3a 30 30"
"-----------------------------------------"
"day regular:   8:30"
"day from(hex): c2 a0 c2 a0 38 3a 33 30"
"-----------------------------------------"
"day regular:   6:00"
"day_to(hex): c2 a0 c2 a0 36 3a 30 30"
"-----------------------------------------"
"day regular:   8:30"
"day from(hex): c2 a0 c2 a0 38 3a 33 30"
"-----------------------------------------"
"day regular:   6:00"
"day_to(hex): c2 a0 c2 a0 36 3a 30 30"
"-----------------------------------------"
"day regular:   8:30"
"day from(hex): c2 a0 c2 a0 38 3a 33 30"
"-----------------------------------------"
"day regular:   6:00"
"day_to(hex): c2 a0 c2 a0 36 3a 30 30"
"-----------------------------------------"
"day regular:   8:30"
"day from(hex): c2 a0 c2 a0 38 3a 33 30"
"-----------------------------------------"
"day regular:   6:00"
"day_to(hex): c2 a0 c2 a0 36 3a 30 30"
"-----------------------------------------"
"day regular:   8:30"
"day from(hex): c2 a0 c2 a0 38 3a 33 30"
"-----------------------------------------"
"day regular:   4:00"
"day_to(hex): c2 a0 c2 a0 34 3a 30 30"
"-----------------------------------------"
"day regular: 10:00"
"day from(hex): 31 30 3a 30 30"
"-----------------------------------------"
"day regular:   3:00"
"day_to(hex): c2 a0 c2 a0 33 3a 30 30"
"-----------------------------------------"

2 个答案:

答案 0 :(得分:4)

您的文件中看起来有一些不间断的空格字符(C2 A0是U + 00A0的UTF-8编码,这是非破坏空间。)

在Ruby中,正则表达式\s与非中断空格不匹配,这就是为什么您的代码看起来不起作用的原因(strip也不会删除它们)。您可以使用\p{Space} character property insteadPOSIX bracket expression [[:space:]]

day_to = day_to.gsub(/\p{Space}/,'') unless day_to.nil?

day_to = day_to.gsub(/[[:space:]]/,'') unless day_to.nil?

答案 1 :(得分:0)

我不确定为什么它不适合你。我运行你的脚本,它对我有用。

但你可以尝试strip,它会删除前导和尾随空格:

day_to.strip! unless day_to.nil?