如何将句子中单词的每个首字母大写?

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));

这篇关于如何将句子中单词的每个首字母大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)