如何在C#中将字符串转换为标题大小写?
标题大小写是主要标题的首字母大写的任何文本,例如标题或标题。标题大小写或标题大小写是一种大写形式,用于以英语呈现已发表的作品或艺术品的标题。使用标题大小写时,除“次要”单词外,所有单词均大写,除非它们是单词的首字母或尾字母。标题。
示例中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