C# 为什么不是';是否有一个本机字符串方法来更改字符串标题大小写?

C# 为什么不是';是否有一个本机字符串方法来更改字符串标题大小写?,c#,string,function,C#,String,Function,在最近的一个项目中一直困扰着我的东西;为什么C#没有一个本机函数将字符串更改为标题大小写 e、 g 我们有.ToLower和.ToUpper,我很感激您可以使用System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase或创建一个TextInfo对象(相同的过程),但它只是。。。丑陋的 有人知道原因吗?事实上: 为什么会在那里?因为外壳取决于当前的文化。例如,土耳其文化中“i”和“i”字符的大小写不同。请阅读更

在最近的一个项目中一直困扰着我的东西;为什么C#没有一个本机函数将字符串更改为标题大小写

e、 g

我们有
.ToLower
.ToUpper
,我很感激您可以使用
System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase
或创建一个TextInfo对象(相同的过程),但它只是。。。丑陋的

有人知道原因吗?

事实上:

为什么会在那里?因为外壳取决于当前的文化。例如,土耳其文化中“i”和“i”字符的大小写不同。请阅读更多关于此的信息

更新:事实上我同意@Cashley的说法,在
String
类上缺少
ToTitleCase
方法看起来像是微软的疏忽。如果您查看
String.ToUpper()
String.ToLower()
您将看到两者都在内部使用
TextInfo

public string ToUpper()
{
    return this.ToUpper(CultureInfo.CurrentCulture);
}

public string ToUpper(CultureInfo culture)
{
    if (culture == null)    
        throw new ArgumentNullException("culture");

    return culture.TextInfo.ToUpper(this);
}
因此,我认为对
ToTitleCase()
应该有相同的方法。也许.NET团队决定只向
String
类添加那些常用的方法。我看不出有任何其他理由让
ToTitleCase
远离。

在这里,但这不是特定于文化的

可能更好的实现只是像String.ToUpper一样包装CurrentCulture.TextInfo:

class Program
{
    static void Main(string[] args)
    {
        string s = "test Title cAsE";
        s = s.ToTitleCase();
        Console.Write(s);
    }
}
public static class StringExtensions
{
    public static string ToTitleCase(this string inputString)
    {
        return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(inputString);
    }

    public static string ToTitleCase(this string inputString, CultureInfo ci)
    {
        return ci.TextInfo.ToTitleCase(inputString);
    }
}


它是CultureInfo的一部分,因为它因区域性而异。但是ToUpper/ToLower也因区域性而异(或者应该这样做)。您可以对string类进行这个精确的扩展,并使用它。为什么你不呢?@loxdog注意到,即使是
TextInfo.ToTitleCase
也不会正确处理你的案例-因为
都是大写字母,它将其视为首字母缩略词,并将其保留在大写字母中。那么为什么touper和ToLower不被降级为
TextInfo
,因为他们有相同的文化问题?看起来-我想这只是MS的疏忽,没有在String中添加ToTitleCase。这就解释了!我会认为这是微软的疏忽,但如果他们解决了这件事就好了…@loxdog坦率地说,我经常使用
ToLower
ToUpper
,但我一生中使用过一两次
ToTitleCase
,所以使用频率似乎是将此方法排除在String类之外的一个很好的理由。所以,也许这是故意的。我想只有微软的人才能肯定这是真的;我通常会从数据源执行,或者在提供数据时强制执行。我想这只是一个不寻常的情况吧!
class Program
{
    static void Main(string[] args)
    {
        string s = "test Title cAsE";
        s = s.ToTitleCase();
        Console.Write(s);
    }
}
public static class StringExtensions
{
    public static string ToTitleCase(this string inputString)
    {
        return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(inputString);
    }

    public static string ToTitleCase(this string inputString, CultureInfo ci)
    {
        return ci.TextInfo.ToTitleCase(inputString);
    }
}
  Dim myString As String = "wAr aNd pEaCe"

  ' Creates a TextInfo based on the "en-US" culture.
  Dim myTI As TextInfo = New CultureInfo("en-US", False).TextInfo

  ' Changes a string to titlecase.
  Console.WriteLine("""{0}"" to titlecase: {1}", myString, myTI.ToTitleCase(myString))
  string myString = "wAr aNd pEaCe";

  // Creates a TextInfo based on the "en-US" culture.
  TextInfo myTI = new CultureInfo("en-US", false).TextInfo;

  // Changes a string to titlecase.
  Console.WriteLine("\"{0}\" to titlecase: {1}", myString, myTI.ToTitleCase(myString));