String to Proper Case in C++/CLI

Posted by: jorgedbucaran on: February 9, 2008

Unfortunately there is no String::ToProperCase() in the .NET Framework, but there is TextInfo::ToTitleCase which is not that bad and actually provides international support. Can’t help a chuckle from that statement. However, if you don’t like:

TextInfo ^texti = gcnew CultureInfo("en-US", false)->TextInfo

You can always implement your own simplified version. I share mine here:

String ^toTitleCase(String ^string)
{
    Text::StringBuilder ^stringb = gcnew Text::StringBuilder();
    Char lastch = ' ';
    for each (Char ch in string) {
        if (Char::IsLetter(ch) && Char::IsWhiteSpace(lastch)) {
            ch = Char::ToUpper(ch);
        }
        lastch = ch;
        stringb->Append(ch);
    }
    return stringb->ToString();
}
Tags: ,

Leave a Reply


  • None
  • Selene: Buahaha, you wrote savage on purpose! XD Nice, so you made it! That's great!

Categories