TextInfo.ToTitleCase(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将指定的字符串转换为标题大小写(大写的单词除外,这些单词被视为首字母缩略词)。
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 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 | 法语 | Les Naufragés d'Ythaq | Les Naufragés D'ythaq |
如上所述,该方法 ToTitleCase 提供了一种任意大小写行为,该行为不一定在语言上正确。 在语言上正确的解决方案需要其他规则,并且当前算法更简单、更快。 我们保留将来使此 API 变慢的权利。
该方法的 ToTitleCase 当前实现将生成与输入字符串相同的长度的输出字符串。 但是,此行为是不能保证的,并且将来的实现可能会更改。