Java 相当于不变文化

发布于 2024-10-22 03:13:44 字数 261 浏览 3 评论 0原文

我正在将以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花辞树 2024-10-29 03:13:44

更新:Java 6 引入了 Locale.ROOT 描述为:

这被视为所有区域设置的基本区域设置,并用作区域设置敏感操作的语言/国家中立区域设置。

这可能比使用 US 更好,但我还没有对照下面的代码进行检查。


不,这基本上是正确的方法。虽然美国文化和不变文化在格式方面存在差异,但我不认为它们会影响大小写规则。

编辑:实际上,一个快速测试程序显示,美国文化中的 .NET 中的大写字符与不变文化中的大写字符不同:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        CultureInfo us = new CultureInfo("en-US");
        for (int i = 0; i < 65536; i++)
        {
            char c = (char) i;
            string s = c.ToString();
            if (s.ToUpperInvariant() != s.ToUpper(us))
            {
                Console.WriteLine(i.ToString("x4"));
            }
        }
    }    
}

输出:

00b5
0131
017f
01c5
01c8
01cb
01f2
0345
0390
03b0
03c2
03d0
03d1
03d5
03d6
03f0
03f1
03f5
1e9b
1fbe

我没有时间查看这些现在,但值得调查。我不知道同样的差异是否适用于 Java - 您可能想获取其中的样本并找出您希望代码执行的操作。

编辑:为了完整,值得一提的是,这只检查单个字符......而您实际上是大写整个字符串,这可能会产生影响。

查看大写的 Java 代码,它似乎仅对 tr、az 和 lt 国家/地区具有特定于区域设置的行为。我知道tr是土耳其,但我不知道其他的......

Update: Java 6 introduced Locale.ROOT which is described as:

This is regarded as the base locale of all locales, and is used as the language/country neutral locale for the locale sensitive operations.

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:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        CultureInfo us = new CultureInfo("en-US");
        for (int i = 0; i < 65536; i++)
        {
            char c = (char) i;
            string s = c.ToString();
            if (s.ToUpperInvariant() != s.ToUpper(us))
            {
                Console.WriteLine(i.ToString("x4"));
            }
        }
    }    
}

Output:

00b5
0131
017f
01c5
01c8
01cb
01f2
0345
0390
03b0
03c2
03d0
03d1
03d5
03d6
03f0
03f1
03f5
1e9b
1fbe

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...

药祭#氼 2024-10-29 03:13:44

这看起来是在不使用任何区域设置的情况下可以获得的最不变的。
如果您关心扩展的 Unicode(过去的 UTF16),您将需要使用 codePoint 解决方案(如果您不知道代码点,则不需要它:))

 static String toUpperCase(String s){
    char[] c = s.toCharArray();
    for (int i=0;i<c.length;i++){
        c[i]=Character.toUpperCase(c[i]);
    }
    return String.copyValueOf(c);  
 }

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 :) )

 static String toUpperCase(String s){
    char[] c = s.toCharArray();
    for (int i=0;i<c.length;i++){
        c[i]=Character.toUpperCase(c[i]);
    }
    return String.copyValueOf(c);  
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文