近日使用ruby获取设备的短信,由于获取到的字符有乱码导致对字符串的处理程序报错
file_content.gsub(/dr/i,'med')
program.rb:4:in `gsub': invalid byte sequence in UTF-8 (ArgumentError)
其他操作也一样报错
可以使用如下程序处理:
if !file_content.valid_encoding?
s = file_content.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
s.gsub(/dr/i,'med')
end
网友评论