我认为最简单的做法是编写一个函数来扫描传递的用于连接单词(and、or、of)的字符串,并将其他所有内容大写。我不知道 .net 中有任何东西可以处理它。
写得真快,它很脏但有效。
using System;
using System.Globalization;
class theclass
{
static void Main()
{
string str1 = "war and peace";
string str2 = "mr. popper's penguins";
Console.WriteLine(Titlizing(str1));
Console.WriteLine(" ");
Console.WriteLine(Titlizing(str2));
}
public static string Titlizing( string toTitle )
{
string[] dawords = toTitle.Split(' ');
string output = "";
TextInfo myTI = new CultureInfo("en-US",false).TextInfo;
foreach (string s in dawords)
{
switch(s)
{
case "and":
output += ' '+ s;
break;
case "or":
output += ' '+ s;
break;
case "of":
output += ' '+ s;
break;
default:
{
output += ' '+myTI.ToTitleCase(s);
break;
}
}
}
return output;
}
}
当我通过战争与和平时,我收到了“战争与和平”。
希望有帮助。