How can I capitalize each first letter of a word in a sentence?(如何将句子中单词的每个首字母大写?)
问题描述
可能重复:
如何将每个句子的首字母大写?
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));
这篇关于如何将句子中单词的每个首字母大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将句子中单词的每个首字母大写?
基础教程推荐
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
