Export (0) Print
Expand All

TextInfo.ToTitleCase Method

Converts the specified string to titlecase.

[Visual Basic]
Public Function ToTitleCase( _
   ByVal str As String _
) As String
[C#]
public string ToTitleCase(
 string str
);
[C++]
public: String* ToTitleCase(
 String* str
);
[JScript]
public function ToTitleCase(
   str : String
) : String;

Parameters

str
The string to convert to titlecase.

Return Value

The specified string converted to titlecase.

Exceptions

Exception Type Condition
ArgumentNullException str is a null reference (Nothing in Visual Basic).

Remarks

Generally, title casing converts the first character of a word to uppercase and converts the rest of the letters to lowercase.

The returned string might differ in length from the input string. For more information on casing, refer to the Unicode Technical Report #21 "Case Mappings," published by the Unicode Consortium (http://www.unicode.org). The current implementation preserves the length of the string; however, this behavior is not guaranteed and could change in future implementations.

Casing semantics depend on the culture in use. If using the invariant culture, the casing semantics are not culture-sensitive. If using a specific culture, the casing semantics are sensitive to that culture. Words that are selected for title casing depend on the language.

If a security decision depends on a string comparison or a case-change operation, use the InvariantCulture to ensure that the behavior will be consistent regardless of the culture settings of the system. However, the invariant culture must be used only by processes that require culture-independent results, such as system services; otherwise, it produces results that might be linguistically incorrect or culturally inappropriate.

For more information on cultures, see CultureInfo.

Example

[Visual Basic, C#, C++] The following code example changes the casing of a string based on the "en-US" culture.

[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

*/

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

int main()
{
   // Defines the String* with mixed casing.
   String* myString = S"wAr aNd pEaCe";

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

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

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

   // Changes a String* to titlecase.
   Console::WriteLine(S"\"{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

*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

TextInfo Class | TextInfo Members | System.Globalization Namespace | ToLower | ToUpper

Show:
© 2014 Microsoft