在 C# 中,ToUpper() 和 ToUpperInvariant() 有什么区别? [英] In C# what is the difference between ToUpper() and ToUpperInvariant()?

查看:47
本文介绍了在 C# 中,ToUpper() 和 ToUpperInvariant() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,ToUpper()ToUpperInvariant()有什么区别?

In C#, what is the difference between ToUpper() and ToUpperInvariant()?

您能举一个结果可能不同的例子吗?

Can you give an example where the results might be different?

推荐答案

ToUpper 使用当前文化.ToUpperInvariant 使用不变的文化.

ToUpper uses the current culture. ToUpperInvariant uses the invariant culture.

典型的例子是土耳其,其中i"的大写不是I".

The canonical example is Turkey, where the upper case of "i" isn't "I".

显示差异的示例代码:

using System;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;

public class Test
{
    [STAThread]
    static void Main()
    {
        string invariant = "iii".ToUpperInvariant();
        CultureInfo turkey = new CultureInfo("tr-TR");
        Thread.CurrentThread.CurrentCulture = turkey;
        string cultured = "iii".ToUpper();

        Font bigFont = new Font("Arial", 40);
        Form f = new Form {
            Controls = {
                new Label { Text = invariant, Location = new Point(20, 20),
                            Font = bigFont, AutoSize = true},
                new Label { Text = cultured, Location = new Point(20, 100),
                            Font = bigFont, AutoSize = true }
            }
        };        
        Application.Run(f);
    }
}

有关土耳其语的更多信息,请参阅此 土耳其测试博文.

For more on Turkish, see this Turkey Test blog post.

听到关于省略字符等还有各种其他大写问题,我不会感到惊讶.这只是我知道的一个例子......部分是因为它在几年前在 Java 中咬了我,在哪里我将字符串大写并将其与MAIL"进行比较.这在土耳其并不奏效......

I wouldn't be surprised to hear that there are various other capitalization issues around elided characters etc. This is just one example I know off the top of my head... partly because it bit me years ago in Java, where I was upper-casing a string and comparing it with "MAIL". That didn't work so well in Turkey...

这篇关于在 C# 中,ToUpper() 和 ToUpperInvariant() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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