Something that’s been bugging me while working on a recent project; why doesn’t C# have a native function to change a string to title case?
e.g.
string x = "hello world! THiS IS a Test mESSAGE";
string y = x.ToTitle(); // y now has value of "Hello World! This Is A Test Message"
We have .ToLower and .ToUpper, and I appreciate you can use System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase or create a TextInfo object (the same process), but it’s just so… ugly.
Anyone know the reason?
Actually it has: TextInfo.ToTitleCase
Why it is there? Because casing depends on current culture. E.g. Turkish cultures have different uppercase and lowercase for ‘i’ and ‘I’ characters. Read more about this here.
UPDATE: Actually I agree with @Cashley which says that missing
ToTitleCasemethod onStringclass looks like an oversight of MicroSoft. If you will look onString.ToUpper()orString.ToLower()you will see that both useTextInfointernally:So, I think there should be same method for
ToTitleCase(). Maybe .NET team decided to add toStringclass only those methods, which are mostly used. I don’t see any other reason for keepingToTitleCaseaway.