TextInfo.ToTitleCase(String) Méthode

Définition

Convertit la chaîne spécifiée en une chaîne avec la première lettre des mots en majuscule (excepté pour les mots qui sont entièrement en majuscules, lesquels sont considérés comme des acronymes).

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

Paramètres

str
String

Chaîne à convertir en chaîne avec la première lettre des mots en majuscule.

Retours

String

Chaîne spécifiée convertie en chaîne avec la première lettre des mots en majuscule.

Exceptions

str a la valeur null.

Exemples

L’exemple suivant modifie la casse d’une chaîne basée sur la culture anglais (États-Unis), avec le nom de culture 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.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

L'exemple suivant passe chaque chaîne d'un tableau à la méthode ToTitleCase. Les chaînes incluent des chaînes de titre appropriées, ainsi que des acronymes. Les chaînes sont converties en majuscules à l’aide des conventions de la culture 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

Remarques

En règle générale, la casse des titres convertit le premier caractère d’un mot en majuscules et le reste des caractères en minuscules. Toutefois, cette méthode ne fournit pas actuellement de casse appropriée pour convertir un mot entièrement en majuscules, tel qu’un acronyme. Le tableau suivant montre la façon dont la méthode restitue plusieurs chaînes.

Entrée Langage Résultat attendu Résultat réel
guerre et paix Anglais Guerre et paix Guerre et paix
Par Anhalter durch die Galaxis Allemand Par Anhalter durch die Galaxis Par Anhalter durch die Galaxis
le naufragés d’ythaq Français Le naufragés d’Ythaq Le naufragés D’ythaq

Comme illustré ci-dessus, la ToTitleCase méthode fournit un comportement de casse arbitraire qui n’est pas nécessairement linguistiquement correct. Une solution linguistiquement correcte nécessiterait des règles supplémentaires et l’algorithme actuel est un peu plus simple et plus rapide. Nous nous réservons le droit de rendre cette API plus lente à l’avenir.

L’implémentation actuelle de la ToTitleCase méthode produit une chaîne de sortie qui a la même longueur que la chaîne d’entrée. Toutefois, ce comportement n’est pas garanti et peut changer dans une implémentation ultérieure.

S’applique à

Voir aussi