TextInfo.ToTitleCase(String) Yöntem

Tanım

Belirtilen dizeyi başlık büyük/küçük harfe dönüştürür (tamamen büyük harf olan ve kısaltma olarak kabul edilen sözcükler dışında).

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

Parametreler

str
String

Başlık büyük/küçük harfe dönüştürülecek dize.

Döndürülenler

Belirtilen dize büyük/küçük harfe dönüştürüldü.

Özel durumlar

str, null değeridir.

Örnekler

Aşağıdaki örnek, İngilizce (Birleşik Devletler) kültürüne göre bir dizenin büyük/küçük harflerini en-US kültür adıyla değiştirir.

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

Aşağıdaki örnek, bir dizideki her dizeyi yöntemine ToTitleCase geçirir. Dizeler hem doğru başlık dizelerini hem de kısaltmaları içerir. Dizeler, en-US kültürünün kuralları kullanılarak başlık büyük/küçük harfe dönüştürülür.

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

Açıklamalar

Genellikle, başlık büyük/küçük harf sözcüğünün ilk karakterini büyük harfe, kalan karakterleri ise küçük harfe dönüştürür. Ancak, bu yöntem şu anda kısaltma gibi tamamen büyük harfli bir sözcüğü dönüştürmek için uygun büyük/küçük harf sağlamaz. Aşağıdaki tabloda, yönteminin çeşitli dizeleri işleme yöntemi gösterilmektedir.

Giriş Dil Beklenen sonuç Gerçek sonuç
savaş ve barış İngilizce Savaş ve Barış Savaş ve Barış
Per anhalter durch die Galaxis Almanca Per Anhalter durch die Galaxis Per Anhalter Durch Die Galaxis
les naufragés d'ythaq Fransızca Les Naufragés d'Ythaq Les Naufragés D'ythaq

Yukarıda gösterildiği gibi yöntemi, ToTitleCase dilsel olarak doğru olması gerekmeyen rastgele bir büyük/küçük harf davranışı sağlar. Dilsel olarak doğru bir çözüm ek kurallar gerektirir ve geçerli algoritma biraz daha basit ve daha hızlıdır. Gelecekte bu API'yi daha yavaş hale getirme hakkını saklıyoruz.

yönteminin ToTitleCase geçerli uygulaması, giriş dizesiyle aynı uzunlukta bir çıkış dizesi verir. Ancak, bu davranış garanti değildir ve gelecekteki bir uygulamada değişebilir.

Şunlara uygulanır

Ayrıca bkz.