可能重复:
如何将每个句子的首字母大写?
public static string CapitalizeEachWord(this string sentence)
{
string[] words = sentence.Split();
foreach (string word in words)
{
word[0] = ((string)word[0]).ToUpper();
}
}
我正在尝试为我正在尝试为自己为未来项目创建的帮助类创建扩展方法.
I'm trying to create a extension method for a helper class I'm trying to create for myself for future projects.
这一项应该适当地大写每个单词.意思是,每个单词的第一个字母都应该大写.我无法让它工作.
This one particular is supposed to capitalize each word appropriately. Meaning, the first letter of every word should be capitalized. I'm having trouble getting this to work.
它说我无法将 char 转换为字符串,但我记得在某些时候能够做到这一点.也许我忘记了一个关键部分.
It says I cannot convert a char to a string, but I remember being able to do that at some point. Maybe I'm forgetting a crucial part.
感谢您的建议.
可能使用TextInfo
类中的ToTitleCase
方法
如何使用 Visual C# 将字符串转换为小写、大写或标题(正确)大小写
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
Console.WriteLine(textInfo.ToTitleCase(title));
这篇关于如何将句子中单词的每个首字母大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!