如何将RichTextBox.Text的每个单词中的第一个字母切换为大写?

时间:2011-10-03 01:37:29

标签: c#

如何将RichTextBox.Text的每个单词的第一个字母切换为大写?

例如,我需要切换此文本:

"This is a big and beautiful dog."

致本文:

"This is a Big And Beautiful Dog".

这意味着我需要将包含三个或更多字母的单词中的第一个字母大写。对我来说很难。此外,RichTextBox.Text中有许多行。

4 个答案:

答案 0 :(得分:2)

试试这个:

System.Globalization.CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Globalization.TextInfo textInfo = cultureInfo.TextInfo;
richTextBox1.Text = textInfo.ToTitleCase(RichTextBox.Text);

答案 1 :(得分:1)

此页面包含有关在忽略文章和介词时转换为正确大小写的详细信息:

http://omegacoder.com/?p=575

答案 2 :(得分:1)

试试这个。

string[] str = richTextBox1.Text.Split(' ');
string a="";
string b="";
for (int i = 0; i < str.Length; i++)
{
    if (str.GetValue(i).ToString().Length > 2)
    {
        b = str.GetValue(i).ToString().Replace(str.GetValue(i).ToString().Substring(0, 1),   str.GetValue(i).ToString().Substring(0, 1).ToUpper());
    }
    else
    {
        b = str.GetValue(i).ToString();
    }
    a = a + " " + b;              
}
richTextBox1.Text = a;

答案 3 :(得分:0)

您只需使用以下内容,即可根据CultureInfo 将每个单词的首字母大写:

注意:“test”是一个样本属性,如姓名,姓氏,地址等。

text = string.IsNullOrEmpty(text) ? string.Empty : CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text.ToLower(new CultureInfo("tr-TR", false)));

请注意,此处还有一个额外的空值控制。希望这会有所帮助...