Java 相当于不变文化
我正在将以下 C# 代码转换为 Java。 Java 中是否存在与 .NET 中的文化不变概念相当的概念?
string upper = myString.ToUpperInvariant();
由于不变文化实际上只是美国文化,我可以在 Java 中做类似的事情,但我想知道是否有更好的方法:
String upper = myString.toUpperCase(Locale.US);
I am converting the following C# code to Java. Is there a Java equivalent to the .NET concept of Invariant Culture?
string upper = myString.ToUpperInvariant();
Since the Invariant Culture is really just the US culture, I could just do something like this in Java, but I'm wondering if there is a better way:
String upper = myString.toUpperCase(Locale.US);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:Java 6 引入了
Locale.ROOT
描述为:这可能比使用 US 更好,但我还没有对照下面的代码进行检查。
不,这基本上是正确的方法。虽然美国文化和不变文化在格式方面存在差异,但我不认为它们会影响大小写规则。
编辑:实际上,一个快速测试程序显示,美国文化中的 .NET 中的大写字符与不变文化中的大写字符不同:
输出:
我没有时间查看这些现在,但值得调查。我不知道同样的差异是否适用于 Java - 您可能想获取其中的样本并找出您希望代码执行的操作。
编辑:为了完整,值得一提的是,这只检查单个字符......而您实际上是大写整个字符串,这可能会产生影响。
查看大写的 Java 代码,它似乎仅对 tr、az 和 lt 国家/地区具有特定于区域设置的行为。我知道tr是土耳其,但我不知道其他的......
Update: Java 6 introduced
Locale.ROOT
which is described as:This is probably better than using US, but I haven't checked it against the code below.
No, that's basically the right way to go. While there are differences between the US culture and the invariant culture in terms of formatting, I don't believe they affect casing rules.
EDIT: Actually, a quick test program shows there are characters which are upper-cased differently in .NET in the US culture to in the invariant culture:
Output:
I don't have time to look at these right now, but it's worth investigating. I don't know if the same differences would apply in Java - you probably want to take a sample of them and work out what you want your code to do.
EDIT: And just to be completist, it's worth mentioning that that only checks for individual characters... whereas you're really upper-casing whole strings, which can make a difference.
Looking at the Java code for upper-casing, that appears to only have locale-specific behaviour for tr, az and lt countries. I know that tr is Turkey, but I don't know about the others...
这看起来是在不使用任何区域设置的情况下可以获得的最不变的。
如果您关心扩展的 Unicode(过去的 UTF16),您将需要使用 codePoint 解决方案(如果您不知道代码点,则不需要它:))
This looks the most invariant you can get w/o using any Locale.
If you care for the extended Unicode (past UTF16), you will need to go for codePoint solution (if you don't know about the codepoints you don't need it :) )