如何在 C# 中将字符串转换为标题大小写?
csharpserver side programmingprogramming
标题大小写是指任何文本,例如标题或标题中的主要单词的首字母大写。标题大小写或标题大小写是一种大写样式,用于呈现已出版作品或英文艺术作品的标题。使用标题大小写时,除"次要"单词外,所有单词均大写,除非它们是标题的第一个或最后一个单词。
示例中 ToTitleCase 的当前实现产生的输出字符串与输入字符串的长度相同。
示例 1
class Program{ static void Main(string[] args){ string myString = "wAr aNd pEaCe"; TextInfo myTI = new CultureInfo("en-US", false).TextInfo; Console.WriteLine("\"{0}\" to titlecase: {1}", myString, myTI.ToTitleCase(myString)); Console.ReadLine(); } }
输出
"war and peace" to titlecase: War And Peace
示例 2
class Program{ static void Main(string[] args){ string[] values = { "a tale of three cities", "gROWL rescue", "inside the office", "sports and tennis", "The Return of Christmas Holmes" }; TextInfo ti = CultureInfo.CurrentCulture.TextInfo; foreach (var value in values) Console.WriteLine("{0} −−> {1}", value, ti.ToTitleCase(value)); Console.ReadLine(); } }
输出
a tale of three cities −−> A Tale Of Three Cities gROWL rescue −−> Growl Rescue inside the office −−> Inside The Office sports and tennis −−> Sports And Tennis The Return of Christmas Holmes −−> The Return Of Christmas Holmes