我怎样才能把握句子中的单词的每个第一个字母? [英] How can I capitalize each first letter of a word in a sentence?

查看:136
本文介绍了我怎样才能把握句子中的单词的每个第一个字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
如何利用每个句子的第一个字母?






 公共静态字符串CapitalizeEachWord(此字符串一句)
{
的String []字= sentence.Split();
的foreach(文字串字)
{
字[0] =((串)字[0])ToUpper的()。
}
}



我试图创建一个一个扩展方法辅助类我试图创建为自己的未来的项目。



这一个特定应该适当地为每个字。这意味着,每一个单词的首字母应该大写。我遇到了麻烦这个工作。



它说我不能一个字符转换为字符串,但我记得能够做到,在某些时候。也许我忘了一个关键的组成部分。

您的建议

感谢。


解决方案

也许使用 ToTitleCase 方法在 TextInfo中



How使用Visual C#



字符串转换为小写,大写或标题(适当)情况

 的CultureInfo的CultureInfo = Thread.CurrentThread.CurrentCulture; 
TextInfo中TextInfo中= cultureInfo.TextInfo;

Console.WriteLine(textInfo.ToTitleCase(职称));


Possible Duplicate:
How to capitalize first letter of each 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.

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.

Thanks for the suggestions.

解决方案

Maybe use the ToTitleCase method in the TextInfo class

How to convert strings to lower, upper, or title (proper) case by using Visual C#

CultureInfo cultureInfo   = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;

Console.WriteLine(textInfo.ToTitleCase(title));

这篇关于我怎样才能把握句子中的单词的每个第一个字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆