TextInfo.ToTitleCase(String) 方法

定義

將指定的字串轉換為字首大寫 (除了視為縮略字的完全大寫文字例外)。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

參數

str
String

要轉換為字首大寫的字串。The string to convert to title case.

傳回

轉換為字首大寫的指定字串。The specified string converted to title case.

例外狀況

strnullstr is null.

範例

下列範例會根據英文(美國)文化特性(文化特性名稱 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.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


下列範例會將陣列中的每個字串傳遞給 ToTitleCase 方法。The following example passes each string in an array to the ToTitleCase method. 這些字串包含適當的字首大寫字串和縮略字。The strings include proper title strings as well as acronyms. 字串會使用 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

備註

一般來說,標題大小寫會將單字的第一個字元轉換成大寫,並將其餘字元轉換成小寫。Generally, title casing converts the first character of a word to uppercase and the rest of the characters to lowercase. 不過,這個方法目前不會提供正確的大小寫來轉換全大寫的單字,例如縮寫。However, this method does not currently provide proper casing to convert a word that is entirely uppercase, such as an acronym. 下表顯示方法呈現數個字串的方式。The following table shows the way the method renders several strings.

輸入Input 語言Language 預期的結果Expected result 實際結果Actual result
war 和安心war and peace 英文English War 和安心War and Peace War 和安心War And Peace
每 anhalter 透過骰子 GalaxisPer anhalter durch die Galaxis 德文German 每 Anhalter 透過骰子 GalaxisPer Anhalter durch die Galaxis 每 Anhalter 透過骰子 GalaxisPer Anhalter Durch Die Galaxis
les naufragés d'ythaqles naufragés d'ythaq 法文French Les Naufragés d'YthaqLes Naufragés d'Ythaq Les Naufragés D'ythaqLes Naufragés D'ythaq

如上所ToTitleCase示,方法會提供不一定是語言正確的任意大小寫行為。As illustrated above, the ToTitleCase method provides an arbitrary casing behavior which is not necessarily linguistically correct. 語言正確的解決方案需要額外的規則,而目前的演算法比較簡單且更快速。A linguistically correct solution would require additional rules, and the current algorithm is somewhat simpler and faster. 我們保留了讓此 API 在未來的速度變慢的權利。We reserve the right to make this API slower in the future.

目前的ToTitleCase方法執行會產生與輸入字串長度相同的輸出字串。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.

適用於

另請參閱