Title case conversion of words

Hi.

I've been programming C# for about 9 months now and I want to be able to convert a string which is in upper case to what is known as 'Title Case'.

This is where the first letter of each word in a string is upper case and the remaining letters of each word are in lower case.

Can anyone tell me how to achieve this please

I know how to use ToUpper and ToLower but I don't know if there is a facility to do the Title Case as I can't seem to find it!



Answer this question

Title case conversion of words

  • Radith

    Hi,

    I couldn't get this to compile. I put the using VB = Microsoft.VisualBasic; line in because my form already had the other two lines in anyway. The code wouldn't compile the first of the two Console lines because the Intellisense wouldn't pick up the Strings after the 'VB.'

    Since then, I have found another way using the following:

    using System.Globalization;
    using System.Threading;

    //Get the culture property of the thread.
    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;

    //Create TextInfo object.
    TextInfo textInfo = cultureInfo.TextInfo;

    MyString = ( textInfo.ToTitleCase(MyString.ToLower() ) );

    Some people complained that this method of Title Case didn't always work. I met with the same problem and my input string was entirely upper case. By changing the input string to lower case, then calling this facility, the conversion worked.

    Thanks for trying this. It was an interesting approach and I have never called any part of the Visual Basic system within a C# routine before!


  • Bill YU

    Did you add the reference to the Microsoft.VisualBasic assembly in the Project References

    Anyway, glad you got it working.



  • Alexander Marinov

    If you add a reference to the Microsoft.VisualBasic assembly, you can call the Strings.StrConv function to do this:

    using System;
    using
    System.Collections.Generic;
    using
    VB = Microsoft.VisualBasic;

    namespace
    ConsoleApplication1
    {
    class
    Program
    {
    static void Main(string
    [] args)
    {
    Console.WriteLine(VB.Strings.StrConv("HELLO WORLD", VB.VbStrConv
    .ProperCase, 0));
    Console
    .ReadLine();
    }
    }
    }



  • m.a.fuchs

    You shouldn't need the .ToLower(). Nor the temp objects:

    MyString = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(MyString);

    this could also be abbreviated slightly to

    MyString = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(MyString);



  • cheesenhomer

    In this case it was necessary. I found some messages on various websites saying that this conversion function was unreliable at the best of times.

    When I tried it without the .ToLower() function, the message still came out in upper case. Once the .ToLower() was inserted, it started working!

    Maybe there's a problem relating to the input of completely upper case letters.

    Anyway, I got my function working in the end, that's all that really matters!

    Sean.


  • Title case conversion of words