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

In C# what is the difference between ToUpper() and ToUpperInvariant()?(在 C# 中,ToUpper() 和 ToUpperInvariant() 有什么区别?)
本文介绍了在 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() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)