为什么 TextInfo.ToTitleCase 在字母全部大写的字符串上不能正常工作?
Why TextInfo.ToTitleCase does not work correctly on a string whose letters are all in upper case?
你能看看我的样品吗?
此结果来自以下示例:
var str = @"VIENNA IS A VERY BEAUTIFUL CAPITAL CITY.";
var title = new CultureInfo("en-US", false).TextInfo.ToTitleCase(str.ToLower());
MessageBox.Show(title);
因为节目的语言是土耳其语。我想提请你注意点字母 I。但我们都知道正确的方法应该是这样的:
Vienna Is A Very Beautiful Capital City.
怎样才能得到真正的结果?
string.ToLower
有一个需要 CultureInfo
的重载。 (Link)
试试
var culture = new CultureInfo("en-US", false);
var title = culture.TextInfo.ToTitleCase(str.ToLower(culture));
如果你想用美国文化来表演套管,你需要坚持下去。相反,您目前 lower-casing 当前区域性中的字符串,这是导致问题的原因。
相反,对 lower-casing 和 title-casing 操作使用相同的 TextInfo
:
sing System;
using System.Globalization;
class Program
{
static void Main()
{
CultureInfo.CurrentCulture = new CultureInfo("tr-TR");
var text = "VIENNA IS A VERY BEAUTIFUL CAPITAL CITY.";
// Original code in the question
var title1 = new CultureInfo("en-US", false).TextInfo.ToTitleCase(text.ToLower());
Console.WriteLine(title1); // Contains Turkish "i" characters
// Corrected code
var textInfo = new CultureInfo("en-US", false).TextInfo;
var lower = textInfo.ToLower(text);
var title2 = textInfo.ToTitleCase(lower);
Console.WriteLine(title2); // Correct output
}
}
(这大致等同于 Jens 的回答,但我更喜欢将 TextInfo
用于这两个操作,如果您将它用于任何一个操作,只是为了保持一致性。)
你能看看我的样品吗?
此结果来自以下示例:
var str = @"VIENNA IS A VERY BEAUTIFUL CAPITAL CITY.";
var title = new CultureInfo("en-US", false).TextInfo.ToTitleCase(str.ToLower());
MessageBox.Show(title);
因为节目的语言是土耳其语。我想提请你注意点字母 I。但我们都知道正确的方法应该是这样的:
Vienna Is A Very Beautiful Capital City.
怎样才能得到真正的结果?
string.ToLower
有一个需要 CultureInfo
的重载。 (Link)
试试
var culture = new CultureInfo("en-US", false);
var title = culture.TextInfo.ToTitleCase(str.ToLower(culture));
如果你想用美国文化来表演套管,你需要坚持下去。相反,您目前 lower-casing 当前区域性中的字符串,这是导致问题的原因。
相反,对 lower-casing 和 title-casing 操作使用相同的 TextInfo
:
sing System;
using System.Globalization;
class Program
{
static void Main()
{
CultureInfo.CurrentCulture = new CultureInfo("tr-TR");
var text = "VIENNA IS A VERY BEAUTIFUL CAPITAL CITY.";
// Original code in the question
var title1 = new CultureInfo("en-US", false).TextInfo.ToTitleCase(text.ToLower());
Console.WriteLine(title1); // Contains Turkish "i" characters
// Corrected code
var textInfo = new CultureInfo("en-US", false).TextInfo;
var lower = textInfo.ToLower(text);
var title2 = textInfo.ToTitleCase(lower);
Console.WriteLine(title2); // Correct output
}
}
(这大致等同于 Jens 的回答,但我更喜欢将 TextInfo
用于这两个操作,如果您将它用于任何一个操作,只是为了保持一致性。)