Export (0) Print
Expand All

TextInfo.ToTitleCase Method

Converts the specified string to titlecase.

Namespace: System.Globalization
Assembly: mscorlib (in mscorlib.dll)

public string ToTitleCase (
	string str
)
public String ToTitleCase (
	String str
)
public function ToTitleCase (
	str : String
) : String

Parameters

str

The string to convert to titlecase.

Return Value

The specified string converted to titlecase.

Exception typeCondition

ArgumentNullException

str is a null reference (Nothing in Visual Basic).

Generally, title casing converts the first character of a word to uppercase and the rest of the characters to lowercase. However, a word that is entirely uppercase, such as an acronym, is not converted.

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.

For more information on casing, see "Unicode Technical Report #21, Case Mappings," at the http://go.microsoft.com/fwlink/?=37123.

The following code example changes the casing of a string based on the "en-US" culture.

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

*/

import System.*;
import System.Globalization.*;

public class SamplesTextInfo
{   
    public static void main(String[] args)
    {
        // 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)).get_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));
    } //main
} //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

*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft