TextInfo.ToTitleCase(String) TextInfo.ToTitleCase(String) TextInfo.ToTitleCase(String) TextInfo.ToTitleCase(String) Method

Definición

Convierte la cadena especificada a mayúscula inicial (excepto las palabras que están totalmente en mayúsculas, que se consideran acrónimos).Converts the specified string to title case (except for words that are entirely in uppercase, which are considered to be acronyms).

public:
 System::String ^ ToTitleCase(System::String ^ str);
public string ToTitleCase (string str);
member this.ToTitleCase : string -> string
Public Function ToTitleCase (str As String) As String

Parámetros

str
String String String String

Cadena que se va a convertir en mayúscula inicial.The string to convert to title case.

Devoluciones

Cadena especificada convertida en mayúscula inicial.The specified string converted to title case.

Excepciones

Ejemplos

El ejemplo siguiente cambia el uso de mayúsculas y minúsculas de una cadena según la referencia cultural inglés (Estados Unidos), con el nombre de la referencia cultural en-US.The following example changes the casing of a string based on the English (United States) culture, with the culture name en-US.

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

*/
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

*/
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


En el ejemplo siguiente, se pasa cada cadena de una matriz al método ToTitleCase.The following example passes each string in an array to the ToTitleCase method. Las cadenas incluyen cadenas de título correctas así como acrónimos.The strings include proper title strings as well as acronyms. Las cadenas se convierten a mayúsculas mediante las convenciones de la referencia cultural en-US.The strings are converted to title case by using the conventions of the en-US culture.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] values = { "a tale of two cities", "gROWL to the rescue",
                          "inside the US government", "sports and MLB baseball",
                          "The Return of Sherlock Holmes", "UNICEF and children"};
                                 
      TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
      foreach (var value in values)
         Console.WriteLine("{0} --> {1}", value, ti.ToTitleCase(value));
   }
}
// The example displays the following output:
//    a tale of two cities --> A Tale Of Two Cities
//    gROWL to the rescue --> Growl To The Rescue
//    inside the US government --> Inside The US Government
//    sports and MLB baseball --> Sports And MLB Baseball
//    The Return of Sherlock Holmes --> The Return Of Sherlock Holmes
//    UNICEF and children --> UNICEF And Children
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim values() As String = { "a tale of two cities", "gROWL to the rescue",
                                 "inside the US government", "sports and MLB baseball",
                                 "The Return of Sherlock Holmes", "UNICEF and children"}
                                 
      Dim ti As TextInfo = CultureInfo.CurrentCulture.TextInfo
      For Each value In values
         Console.WriteLine("{0} --> {1}", value, ti.ToTitleCase(value))
      Next
   End Sub
End Module
' The example displays the following output:
'    a tale of two cities --> A Tale Of Two Cities
'    gROWL to the rescue --> Growl To The Rescue
'    inside the US government --> Inside The US Government
'    sports and MLB baseball --> Sports And MLB Baseball
'    The Return of Sherlock Holmes --> The Return Of Sherlock Holmes
'    UNICEF and children --> UNICEF And Children

Comentarios

Por lo general, el título convierte el primer carácter de una palabra a mayúsculas y el resto de los caracteres a minúsculas.Generally, title casing converts the first character of a word to uppercase and the rest of the characters to lowercase. Sin embargo, este método no proporciona actualmente mayúsculas y minúsculas correctas para convertir una palabra que esté completamente en mayúscula, como un acrónimo.However, this method does not currently provide proper casing to convert a word that is entirely uppercase, such as an acronym. En la tabla siguiente se muestra que la forma en que el método representa varias cadenas.The following table shows the way the method renders several strings.

EntradaInput LenguajeLanguage Resultado esperadoExpected result Resultado realActual result
guerra y Pazwar and peace InglésEnglish Guerra y PazWar and Peace Guerra y PazWar And Peace
Por anhalter durch die GalaxisPer anhalter durch die Galaxis AlemánGerman Por Anhalter durch die GalaxisPer Anhalter durch die Galaxis Por Anhalter Durch Die GalaxisPer Anhalter Durch Die Galaxis
les naufragés d'ythaqles naufragés d'ythaq FrancésFrench Les Naufragés d'YthaqLes Naufragés d'Ythaq EEI Naufragés D'ythaqLes Naufragés D'ythaq

Como se ilustra arriba, el ToTitleCase método proporciona un comportamiento de grafía arbitrario que no es necesariamente lingüísticamente correcto.As illustrated above, the ToTitleCase method provides an arbitrary casing behavior which is not necessarily linguistically correct. Una solución correcta lingüísticamente requeriría reglas adicionales y el algoritmo actual es un poco más sencillo y rápido.A linguistically correct solution would require additional rules, and the current algorithm is somewhat simpler and faster. Nos reservamos el derecho para hacer esta API más lenta en el futuro.We reserve the right to make this API slower in the future.

La implementación actual de la ToTitleCase método da como resultado una cadena de salida es la misma longitud que la cadena de entrada.The current implementation of the ToTitleCase method yields an output string that is the same length as the input string. Sin embargo, este comportamiento no se garantiza y podría cambiar en una futura implementación.However, this behavior is not guaranteed and could change in a future implementation.

Se aplica a

Consulte también: