在C#中,ToUpper()
和ToUpperInvariant()
有什么不同
你能举一个结果可能不同的例子吗?
发布于 2010-08-24 01:51:28
ToUpper
使用当前区域性。ToUpperInvariant
使用不变的区域性。
典型的例子是土耳其,"I“的大写字母不是"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);
}
}
有关土耳其语的更多信息,请参阅此Turkey Test blog post。
如果听到关于省略字符等的其他各种大小写问题,我不会感到惊讶。这只是我脑海中的一个例子……部分原因是它在几年前的Java中咬了我一口,当时我正在大写字符串,并将其与"MAIL“进行比较。在土耳其就没那么好用了。
发布于 2010-08-24 01:52:15
ToUpperInvariant
使用invariant culture中的规则
发布于 2010-08-24 01:53:23
英语是没有区别的。只有在土耳其文化中才能发现差异。
https://stackoverflow.com/questions/3550213
复制相似问题