TextInfo.ToTitleCase(String) Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Konwertuje określony ciąg na wielkie litery tytułu (z wyjątkiem wyrazów, które są całkowicie wielkie, które są uważane za akronimy).
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
Parametry
- str
- String
Ciąg, który ma zostać przekonwertowany na literę tytułu.
Zwraca
Określony ciąg przekonwertowany na literę tytułu.
Wyjątki
str
to null
.
Przykłady
W poniższym przykładzie zmieniana jest litera ciągu na podstawie kultury angielskiej (Stany Zjednoczone) z nazwą kultury 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
Poniższy przykład przekazuje każdy ciąg w tablicy do ToTitleCase metody . Ciągi zawierają odpowiednie ciągi tytułu, a także akronimy. Ciągi są konwertowane na przypadek tytułu przy użyciu konwencji kultury 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
Uwagi
Ogólnie rzecz biorąc, litera tytułu konwertuje pierwszy znak wyrazu na wielkie litery, a pozostałe znaki na małe litery. Jednak ta metoda obecnie nie zapewnia odpowiedniej wielkości liter do konwertowania wyrazu, który jest całkowicie wielki, na przykład akronim. W poniższej tabeli przedstawiono sposób renderowania kilku ciągów przez metodę.
Dane wejściowe | Język | Oczekiwany wynik | Rzeczywisty wynik |
---|---|---|---|
war and pokochać | Angielski | War and Omów | War and Omów |
Per anhalter durch die Richxis | Niemiecki | Per Anhalter durch die Richxis | Per Anhalter Durch DieGinxis |
les naufragés d'ythaq | Francuski | Les Naufragés d'Ythaq | Les Naufragés D'ythaq |
Jak pokazano powyżej, metoda zapewnia dowolne zachowanie w wielkości, ToTitleCase które nie musi być językowo poprawne. Poprawne lingwistycznie rozwiązanie wymagałoby dodatkowych reguł, a bieżący algorytm jest nieco prostszy i szybszy. Zastrzegamy sobie prawo do tego, aby ten interfejs API był wolniejszy w przyszłości.
Bieżąca implementacja metody daje ciąg wyjściowy o takiej samej długości ToTitleCase jak ciąg wejściowy. Jednak to zachowanie nie jest gwarantowane i może ulec zmianie w przyszłej implementacji.