示例文件包含此内容。
[" \u042D\u0440\u044B\u0433\u044D\u0436\u044D\u044D. \n"]
我想用实际的utf-8字符替换\u[a-zA-Z0-9]字符。
该怎么办?

File.new("mnosis.xml").each_line do |line|
  re = /\\u[0-9a-fA-F]{4}/
  new_str = line.gsub(re) {|match| puts match[0..1] + '#{' + match[2..5] + '}' }
  puts new_str
end

最佳答案

new_str = line.gsub(re) {|match| Array(match[2..5].to_i(16)).pack('U') }
=> "Эрыгэжээ."

关于ruby - 将字符串转换为实际的UTF-8字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8401243/

10-12 12:52
查看更多