特殊字符大写 [英] Special character uppercase

查看:25
本文介绍了特殊字符大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含一堆特殊字符的字符串.这有效:

I have strings with a bunch of special characters. This works:

myString.upcase.tr('æ-ý','Æ-Ý')

然而,它并不能真正跨平台工作.我在 Windows 上的 Ruby 实现不适合这个(在我的 Mac 和 Linux 机器上,工作起来很有魅力).任何指针/解决方法/解决方案,真的很感激!

However, it does not work really cross-platform. My Ruby implementation on Windows won't go with this (on my Mac and Linux machines, works like a charm). Any pointers / workarounds / solutions, really appreciated!

推荐答案

不幸的是,如果不了解语言,甚至在某些情况下甚至不了解字符串的内容,就不可能正确地大写/小写字符串.

Unfortunately, it is impossible to correctly upcase/downcase a string without knowing the language and it in some cases even the contents of the string.

例如,在英语中,i 的大写变体是 I,而 I 的小写变体是 i>,但在土耳其语中,i 的大写变体是 İ,而 I 的小写变体是 ı.在德语中,ß 的大写变体是 SS,但 ss 的大写变体也是,所以要小写,您需要了解文本,因为例如MASSE 可以缩减为 masse(质量)或 maße(测量).

For example, in English the uppercase variant of i is I and the lowercase variant of I is i, but in Turkish the uppercase variant of i is İ and the lowercase variant of I is ı. In German, the uppercase variant of ß is SS, but so is the uppercase variant of ss, so to downcase, you need to understand the text, because e.g. MASSE could be downcased to either masse (mass) or maße (measurements).

Ruby 采取了简单的方法,只需在 ASCII 字母表中使用大写/小写.

Ruby takes the easy way out and simply only uppercases/downcases within the ASCII alphabet.

然而,这仅解释了为什么需要您的解决方法,而不是为什么它有时有效有时无效.假设您在所有平台上使用相同的 Ruby 版本和相同的 Ruby 实现以及相同版本的实现,它应该工作.YARV 很少使用底层平台的字符串操作例程(对于大多数 Ruby 实现也是如此,实际上,即使 JRuby 也不使用 Java 强大的字符串库,而是为了最大兼容性而推出自己的字符串库),而且它也不使用除了 Onigmo 之外的任何第三方库(例如 ICU),因此不太可能归咎于平台差异.不过,不同版本的 Ruby 使用不同版本的 Unicode 字符数据库(例如,我相信它至少在 1.9 和 2.2 之间更新了一次),所以如果您的版本不匹配,这可能可以解释.

However, that only explains why your workaround is needed, not why it sometimes works and sometimes doesn't. Provided that you use the same Ruby version and the same Ruby implementation and the same version of the implementation on all platforms, it should work. YARV doesn't use the underlying platform's string manipulation routines much (the same is true for most Ruby implementations, actually, even JRuby doesn't use Java's powerful string libraries but rolls its own for maximum compatibility), and it also doesn't use any third-party libraries (like e.g. ICU) except Onigmo, so it's unlikely that platform differences are to blame. Different versions of Ruby use different versions of the Unicode Character Database, though (e.g. I believe it was updated somewhere between 1.9 and 2.2 at least once), so if you have a version mismatch, that might explain it.

或者,它可能是 Windows 上 YARV 中的真正错误.也许试试 JRuby?它在平台之间往往更一致,实际上,在 Windows 上,它与 Ruby 的兼容性比 Ruby(即 YARV)本身更兼容!

Or, it might be a genuine bug in YARV on Windows. Maybe try JRuby? It tends to be more consistent between platforms, in fact, on Windows, it is more compatible with Ruby than Ruby (i.e. YARV) itself!

这篇关于特殊字符大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆