TextInfo.ToTitleCase(String) 메서드

정의

지정된 문자열에서 단어의 첫 글자를 대문자로 변환합니다(약어로 간주되는 모두 대문자로 이루어진 단어 제외).

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

첫 글자만 대문자로 변환할 문자열입니다.

반환

첫 글자만 대문자로 변환된 지정된 문자열입니다.

예외

str이(가) null인 경우

예제

다음 예제에서는 영어(미국) 문화권에 따라 문자열의 대/소문자를 문화권 이름 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 메서드에 전달합니다. 문자열에는 적절한 제목 문자열과 머리글자어가 포함됩니다. 문자열은 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

설명

일반적으로 제목 대/소문자는 단어의 첫 문자를 대문자로 변환하고 나머지 문자는 소문자로 변환합니다. 그러나 이 메서드는 현재 약어와 같이 완전히 대문자인 단어를 변환하기 위한 적절한 대/소문자를 제공하지 않습니다. 다음 표에서는 메서드가 여러 문자열을 렌더링하는 방법을 보여 줍니다.

입력 언어 예상된 결과 실제 결과
전쟁과 평화 영어 전쟁과 평화 전쟁과 평화
Per anhalter durch die Galaxis 독일어 Per Anhalter durch die Galaxis Per Anhalter Durch Die Galaxis
les naufragés d'ythaq 프랑스어 레 나우프라게스 디타크 레 나우프라게스 디타크

위에서 설명한 것처럼 메서드는 ToTitleCase 언어적으로 올바르지 않은 임의의 대/소문자 동작을 제공합니다. 언어적으로 올바른 솔루션에는 추가 규칙이 필요하며 현재 알고리즘은 다소 간단하고 빠릅니다. 향후 이 API를 더 느리게 만들 수 있는 권리가 있습니다.

메서드의 ToTitleCase 현재 구현은 입력 문자열과 길이가 같은 출력 문자열을 생성합니다. 그러나 이 동작은 보장되지 않으며 향후 구현에서 변경될 수 있습니다.

적용 대상

추가 정보