How To: Capitalize the first letter of every word in a string

Ever thought of how to capitalize the first letter of every word in a string? Here's how:

[C#]

using System;
using System.Globalization;

namespace KeithRull.CapitalizingLetters
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string textToTransform = "keith, you need to post more blogs!";

            Console.WriteLine("Original text: " + textToTransform);

            //capitalizing the first letter of our text using the current culture
            Console.WriteLine("Capitalize using the current culture: "
                + CultureInfo.CurrentCulture.TextInfo.ToTitleCase(textToTransform));

            //capitalizing the first letter of our text using a defined culture
            CultureInfo newCultureInfo = new CultureInfo("zh-Hans", false);
            TextInfo textInfo = newCultureInfo.TextInfo;
            Console.WriteLine("Capitalize using a specified culture: " + textInfo.ToTitleCase(textToTransform));

            Console.Read();
        }
    }
}

[VB.NET]

Imports System
Imports System.Globalization
 
Namespace KeithRull.CapitalizingLetters
    Friend Class Program
        Shared  Sub Main(ByVal args() As String)
            Dim textToTransform As String =  "keith, you need to post more blogs!" 
 
            Console.WriteLine("Original text: " + textToTransform)
 
            'capitalizing the first letter of our text using the current culture
            Console.WriteLine("Capitalize using the current culture: "
                Dim CultureInfo.CurrentCulture.TextInfo.ToTitleCase(textToTransform)) As +
 
            'capitalizing the first letter of our text using a defined culture
            Dim NewCultureInfo As CultureInfo =  New CultureInfo("zh-Hans",False) 
            Dim textInfo As TextInfo =  NewCultureInfo.TextInfo 
            Console.WriteLine("Capitalize using a specified culture: " + textInfo.ToTitleCase(textToTransform))
 
            Console.Read()
        End Sub
    End Class
End Namespace

HTH


Posted Aug 19 2008, 10:59 AM by keithrull

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:

Copyright DevPinoy 2005-2008