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

定義

指定した文字列を (頭字語と見なされる、すべてが大文字で記載された単語を除き)、英語見出しと同じ大文字/小文字の組み合わせに変換します。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 String String String

単語の先頭文字を大文字に変換する文字列。The string to convert to title case.

戻り値

指定した文字列内の単語の先頭文字が大文字に変換された文字列。The specified string converted to title case.

例外

次の例では、英語 (米国) カルチャ、カルチャ名 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


次の例は、配列内の各文字列を 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 and peace 英語English 戦争と平和War and Peace 戦争と平和War And Peace
Anhalter durch サイコロ Galaxis ごとPer anhalter durch die Galaxis ドイツ語German Anhalter durch サイコロ Galaxis ごとPer Anhalter durch die Galaxis Anhalter Durch サイコロ Galaxis ごとPer 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.

適用対象

こちらもご覧ください