我想将我的字符串格式化为大写字符格式(即大写单词的第一个字符)。
For Example -
If Input is: "NEW YORK CITY"
then the desired output is: "New York City"
*我的字符串最多有 3 个单词。
之后,用谷歌搜索我发现了几种实现这种方法的方法,但我不知道哪种方法是最好的。
方法一:
string City = "NEW YORK CITY";
City = City.ToLower();
string Capatilize_City = "";
Capatilize_City = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(City);
方法二:
string City = "NEW YORK CITY";
string[] lstWord = City.ToLower().Split(' ');
string Capatilize_City = "";
foreach (string s in lstWord)
{
string z = s.Substring(0, 1).ToUpper() + s.Substring(1, s.Length - 1);
Capatilize_City += " " + z;
}
Capatilize_City = Capatilize_City.Trim();
哪种代码最适合使用(性能和速度副)?
最佳答案
您应该关注代码大小、可读性、可理解性、可维护性,因此明显的赢家是...
编辑
所以我建议
Capatilize_City =
System.Globalization.CultureInfo.CurrentCulture.TextInfo
.ToTitleCase(City.ToLower());
关于c# - 将字符串中单词的第一个字符大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27978506/