如何在 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
广告