我正在解析Wikipedia上的一些图片链接。我在http://en.wikipedia.org/wiki/Special:Export/Diego_Forl%C3%A1n上遇到了这个

当我使用不赞成使用的URLEncoder.encode时,我可以正确编码带重音符号的字符,但是当我指定“ UTF-8”参数时,它将失败。维基百科上的文字是utf8 AFAIK。

Diego + Forl%C3%A1n + vs + the + Netherlands.jpg是正确的,而Diego + Forl%E2%88%9A%C2%B0n + vs + the + Netherlands.jpg是错误的。

scala> first
res24: String = Diego Forlán vs the Netherlands.jpg

scala> java.net.URLEncoder.encode(first, "UTF-8")
res25: java.lang.String = Diego+Forl%E2%88%9A%C2%B0n+vs+the+Netherlands.jpg

scala> java.net.URLEncoder.encode(first)
<console>:33: warning: method encode in object URLEncoder is deprecated: see corresponding Javadoc for more information.
              java.net.URLEncoder.encode(first)
                                  ^
res26: java.lang.String = Diego+Forl%C3%A1n+vs+the+Netherlands.jpg

最佳答案

我猜想first已经损坏,并且由于控制台配置隐藏的转码错误而只能正确呈现。

您可以通过在字符串中发出UTF-16代码单元来确认这一点:

for(c<-first.toCharArray()){print("\\u%04x".format(c.toInt))}


可能有一种更优雅的书写方式。

如果代码点编码正确,它将是:

U+00e1      á       \u00e1


我希望可以使用MacRoman解码器对UTF-8编码的数据进行解码。

codepoint   glyph   escaped    x-MacRoman     info
=======================================================================
U+221a      √       \u221a     c3,            MATHEMATICAL_OPERATORS, MATH_SYMBOL
U+00b0      °       \u00b0     a1,            LATIN_1_SUPPLEMENT, OTHER_SYMBOL

09-25 18:25
查看更多