首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在C#中,ToUpper()和ToUpperInvariant()有什么不同?

在C#中,ToUpper()和ToUpperInvariant()有什么不同?
EN

Stack Overflow用户
提问于 2010-08-24 01:49:30
回答 3查看 61.3K关注 0票数 142

在C#中,ToUpper()ToUpperInvariant()有什么不同

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-08-24 01:51:28

ToUpper使用当前区域性。ToUpperInvariant使用不变的区域性。

典型的例子是土耳其,"I“的大写字母不是"i”。

显示差异的示例代码:

代码语言:javascript
复制
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);
    }
}

有关土耳其语的更多信息,请参阅此Turkey Test blog post

如果听到关于省略字符等的其他各种大小写问题,我不会感到惊讶。这只是我脑海中的一个例子……部分原因是它在几年前的Java中咬了我一口,当时我正在大写字符串,并将其与"MAIL“进行比较。在土耳其就没那么好用了。

票数 167
EN

Stack Overflow用户

发布于 2010-08-24 01:52:15

ToUpperInvariant使用invariant culture中的规则

票数 3
EN

Stack Overflow用户

发布于 2010-08-24 01:53:23

英语是没有区别的。只有在土耳其文化中才能发现差异。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3550213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档