Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
TextInfo Class
TextInfo Methods
 ToTitleCase Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
TextInfo..::.ToTitleCase Method

Converts the specified string to titlecase.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Function ToTitleCase ( _
    str As String _
) As String
Visual Basic (Usage)
Dim instance As TextInfo
Dim str As String
Dim returnValue As String

returnValue = instance.ToTitleCase(str)
C#
public string ToTitleCase(
    string str
)
Visual C++
public:
String^ ToTitleCase(
    String^ str
)
JScript
public function ToTitleCase(
    str : String
) : String

Parameters

str
Type: System..::.String
The string to convert to titlecase.

Return Value

Type: System..::.String
The specified string converted to titlecase.
ExceptionCondition
ArgumentNullException

str is null.

Generally, title casing converts the first character of a word to uppercase and the rest of the characters to lowercase. However, this method does not currently provide proper casing to convert a word that is entirely uppercase, such as an acronym. The following table shows the way the method renders several strings.

Input

Language

Expected result

Actual result

war and peace

English

War and Peace

War And Peace

Per anhalter durch die Galaxis

German

Per Anhalter durch die Galaxis

Per Anhalter Durch Die Galaxis

les naufragés d'ythaq

French

Les Naufragés d'Ythaq

Les Naufragés D'ythaq

The current implementation of the ToTitleCase()()() method yields an output string that is the same length as the input string. However, this behavior is not guaranteed and could change in a future implementation.

The following code example changes the casing of a string based on the English (United States) culture, with the culture name en-US.

Visual Basic
Imports System
Imports System.Globalization

Public Class SamplesTextInfo

   Public Shared Sub Main()

      ' Defines the string with mixed casing.
      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 lowercase.
      Console.WriteLine("""{0}"" to lowercase: {1}", myString, myTI.ToLower(myString))

      ' Changes a string to uppercase.
      Console.WriteLine("""{0}"" to uppercase: {1}", myString, myTI.ToUpper(myString))

      ' Changes a string to titlecase.
      Console.WriteLine("""{0}"" to titlecase: {1}", myString, myTI.ToTitleCase(myString))

   End Sub 'Main 

End Class 'SamplesTextInfo


'This code produces the following output.
'
'"wAr aNd pEaCe" to lowercase: war and peace
'"wAr aNd pEaCe" to uppercase: WAR AND PEACE
'"wAr aNd pEaCe" to titlecase: War And Peace


C#
using System;
using System.Globalization;


public class SamplesTextInfo  {

   public static void Main()  {

      // Defines the string with mixed casing.
      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 lowercase.
      Console.WriteLine( "\"{0}\" to lowercase: {1}", myString, myTI.ToLower( myString ) );

      // Changes a string to uppercase.
      Console.WriteLine( "\"{0}\" to uppercase: {1}", myString, myTI.ToUpper( myString ) );

      // Changes a string to titlecase.
      Console.WriteLine( "\"{0}\" to titlecase: {1}", myString, myTI.ToTitleCase( myString ) );

   }

}

/*
This code produces the following output.

"wAr aNd pEaCe" to lowercase: war and peace
"wAr aNd pEaCe" to uppercase: WAR AND PEACE
"wAr aNd pEaCe" to titlecase: War And Peace

*/
Visual C++
using namespace System;
using namespace System::Globalization;
int main()
{

   // Defines the String* with mixed casing.
   String^ myString = "wAr aNd pEaCe";

   // Creates a TextInfo based on the S"en-US" culture.
   CultureInfo^ MyCI = gcnew CultureInfo( "en-US",false );
   TextInfo^ myTI = MyCI->TextInfo;

   // Changes a String* to lowercase.
   Console::WriteLine( "\"{0}\" to lowercase: {1}", myString, myTI->ToLower( myString ) );

   // Changes a String* to uppercase.
   Console::WriteLine( "\"{0}\" to uppercase: {1}", myString, myTI->ToUpper( myString ) );

   // Changes a String* to titlecase.
   Console::WriteLine( "\"{0}\" to titlecase: {1}", myString, myTI->ToTitleCase( myString ) );
}

/*
This code produces the following output.

S"wAr aNd pEaCe" to lowercase: war and peace
S"wAr aNd pEaCe" to uppercase: WAR AND PEACE
S"wAr aNd pEaCe" to titlecase: War And Peace

*/

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
ToTitleCase isn't linguistically correct.      Shawn Steele [MSFT]   |   Edit   |   Show History

Note that ToTitleCase() provides an arbitrary casing behavior, not a linguistically correct behavior. For example "War and Peace" as above should have a lower case a in English. In German you'd have "Per Anhalter durch die Galaxis" but in English you'd capitalize the "Durch" (through). In French you'd have "Les Naufragés d'Ythaq", but the ToTitleCase rules would make the d uppercase and the y lowercase.

Part of the reason is that a linguistically correct solution would require additional rules, and the current algorithm is somewhat simple and faster. We reserve the right to make this API slower in the future.

Tags What's this?: Add a tag
Flag as ContentBug
ToTitleCase is NOT ToProperCase      jdrake3   |   Edit   |   Show History
Note that ToTitleCase() does not Proper case strings. So "WAR AND PEACE" will be rendered as "WAR AND PEACE" not "War And Peace" as one may expect from the examples.
Tags What's this?: Add a tag
Flag as ContentBug
How to deal with UPPER CASE      Rory Becker   |   Edit   |   Show History
ToTitleCase is documented as handling UPPER CASE STRINGS differently to allow for abbreviations .

If you need different behaviour, simply use...
-------------------------------------------------------------
ToTitleCase("SOME UPPERCASE STRING".Tolower)
-------------------------------------------------------------
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker