本文介绍了如何使用 ruby 1.9 转换字符编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前在处理 amazon api 的结果时遇到问题.
i am currently having trouble with results from the amazon api.
该服务返回一个包含 unicode 字符的字符串:Mac 上的 Learn ObjectivexE2x80x93C(学习系列)
the service returns a string with unicode characters: Learn ObjectivexE2x80x93C on the Mac (Learn Series)
使用 ruby 1.9.1 甚至无法处理字符串:
with ruby 1.9.1 the string could not even been processed:
REXML::ParseException: #<Encoding::CompatibilityError: incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string)>
...
Exception parsing
Line: 1
Position: 1636
Last 80 unconsumed characters:
Learn Objective–C on the Mac (Learn Series)
推荐答案
作为例外点,您的字符串是 ASCII-8BIT 编码的.您应该更改编码.有一个长篇故事,但如果您对快速解决方案感兴趣,只需force_encoding
在你做任何处理之前:
As the exception points, your string is ASCII-8BIT encoded. You should change the encoding. There is a long story about that, but if you are interested in quick solution, just force_encoding
on the string before you do any processing:
s = "Learn ObjectivexE2x80x93C on the Mac"
# => "Learn ObjectivexE2x80x93C on the Mac"
s.encoding
# => #<Encoding:ASCII-8BIT>
s.force_encoding 'utf-8'
# => "Learn Objective–C on the Mac"
这篇关于如何使用 ruby 1.9 转换字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!