TextInfo.ToTitleCase(String) Metodo

Definizione

Converte la stringa specificata in maiuscolo (ad eccezione delle parole interamente maiuscole, considerate acronimi).

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

Parametri

str
String

Stringa da convertire in lettere maiuscole/minuscole del titolo.

Valori restituiti

Stringa specificata convertita in lettere maiuscole/minuscole del titolo.

Eccezioni

str è null.

Esempio

Nell'esempio seguente viene modificata la combinazione di maiuscole e minuscole di una stringa in base alle impostazioni cultura inglese (Stati Uniti), con il nome delle impostazioni cultura en-US.

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.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

End Class


'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

Nell'esempio seguente viene passata ogni stringa in una matrice al ToTitleCase metodo . Le stringhe includono correttamente i titoli e gli acronimi. Le stringhe vengono convertite in maiuscole e minuscole del titolo usando le convenzioni delle impostazioni cultura en-US.

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

Commenti

In genere, la combinazione di maiuscole e minuscole converte il primo carattere di una parola in maiuscolo e il resto dei caratteri in minuscolo. Tuttavia, questo metodo attualmente non fornisce maiuscole e minuscole appropriate per convertire una parola interamente maiuscola, ad esempio un acronimo. Nella tabella seguente viene illustrato il modo in cui il metodo esegue il rendering di diverse stringhe.

Inserimento Language Risultato previsto Risultato effettivo
guerra e pace English Guerra e pace Guerra e pace
Per anhalter durch die Galaxis Tedesco Per Anhalter durch die Galaxis Per Anhalter Durch Die Galaxis
les naufragés d'ythaq Francese Les Naufragés d'Ythaq Les Naufragés D'ythaq

Come illustrato in precedenza, il ToTitleCase metodo fornisce un comportamento arbitrario di maiuscole e minuscole che non è necessariamente corretto a livello linguistico. Una soluzione linguisticamente corretta richiede regole aggiuntive e l'algoritmo corrente è leggermente più semplice e veloce. Si riserva il diritto di rallentare questa API in futuro.

L'implementazione corrente del ToTitleCase metodo restituisce una stringa di output che corrisponde alla stessa lunghezza della stringa di input. Tuttavia, questo comportamento non è garantito e potrebbe cambiare in un'implementazione futura.

Si applica a

Vedi anche