loxdog
loxdog

Reputation: 1007

Why isn't there a native string method to change a string title case?

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?

Upvotes: 2

Views: 388

Answers (3)

will simmons
will simmons

Reputation: 187

  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))

OR

  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));

Upvotes: 0

Cashley
Cashley

Reputation: 516

There's an extension method for this here, but it's not culture specific.

Possibly better implementation just wrapping CurrentCulture.TextInfo like String.ToUpper:

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);
    }
}

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

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 ToTitleCase method on String class looks like an oversight of MicroSoft. If you will look on String.ToUpper() or String.ToLower() you will see that both use TextInfo internally:

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);
}

So, I think there should be same method for ToTitleCase(). Maybe .NET team decided to add to String class only those methods, which are mostly used. I don't see any other reason for keeping ToTitleCase away.

Upvotes: 2

Related Questions