本文介绍了如何使用ruby 1.9转换字符编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
该服务返回一个包含unicode字符的字符串: Learn Objective\xE2\x80 \x93C在Mac(学习系列)
与ruby 1.9.1字符串甚至不能被处理:
REXML :: ParseException:#< Encoding :: CompatibilityError:不兼容的编码regexp匹配(UTF-8正则表达式与ASCII-8BIT字符串)>
...
异常解析
行:1
位置:1636
最后80个未消耗的字符:
在Mac上学习Objective-C(学习系列)
解决方案
作为异常点,您的字符串是ASCII-8BIT编码的。您应该更改编码。有一个,但如果您对快速解决方案感兴趣,只需 force_encoding
在您执行任何处理之前:
s =学习目标Mac上的\\ xE2\x80\x93C
#=> 在Mac上学习Objective\xE2\x80\x93C
s.encoding
#=> #<编码:ASCII-8BIT>
s.force_encoding'utf-8'
#=> 在Mac上学习Objective-C
i am currently having trouble with results from the amazon api.
the service returns a string with unicode characters: Learn Objective\xE2\x80\x93C on the Mac (Learn Series)
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)
解决方案
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 Objective\xE2\x80\x93C on the Mac"
# => "Learn Objective\xE2\x80\x93C on the Mac"
s.encoding
# => #<Encoding:ASCII-8BIT>
s.force_encoding 'utf-8'
# => "Learn Objective–C on the Mac"
这篇关于如何使用ruby 1.9转换字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!