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

您所在的位置:网站首页 指针字符串中大小写字母转换 如何将句子中单词的每个首字母大写?

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

2023-05-21 09:06| 来源: 网络整理| 查看: 265

本文介绍了如何将句子中单词的每个首字母大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧! 问题描述 限时送ChatGPT账号..

可能重复:如何将每个句子的首字母大写?

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

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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3