如何将RichTextBox.Text
的每个单词的第一个字母切换为大写?如何将RichTextBox.Text的每个单词中的第一个字母切换为大写?
例如,我需要这个文本切换:
"This is a big and beautiful dog."
向该文本:
"This is a Big And Beautiful Dog".
这意味着我需要利用其中包括三个字母或以上的单词的第一个字母。对我来说很难。另外,RichTextBox.Text
中有很多行。
如何将RichTextBox.Text
的每个单词的第一个字母切换为大写?如何将RichTextBox.Text的每个单词中的第一个字母切换为大写?
例如,我需要这个文本切换:
"This is a big and beautiful dog."
向该文本:
"This is a Big And Beautiful Dog".
这意味着我需要利用其中包括三个字母或以上的单词的第一个字母。对我来说很难。另外,RichTextBox.Text
中有很多行。
试试这个。
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;
这个页面有详细资料转换成适当的情况下,而忽略冠词和介词:
试试这个:
System.Globalization.CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Globalization.TextInfo textInfo = cultureInfo.TextInfo;
richTextBox1.Text = textInfo.ToTitleCase(RichTextBox.Text);
输出与user922907想要的不一样。 “这是一条大而美丽的狗”。 –
你的权利,但在这一点上,他将不得不解析每个组件,以检查句子部分的长度,如果> 2做这项工作。 它可以通过一个String.Split来完成 –
您可以通过简单地使用这个根据的CultureInfo为每个字的第一个字母:
注:“测试”是一个示例财产姓名,地址等
text = string.IsNullOrEmpty(text) ? string.Empty : CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text.ToLower(new CultureInfo("tr-TR", false)));
请注意,在这里有一个额外的空值控制。希望这有助于...
这是一个类似的讨论,但不一样http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b7ea95d7-f27b-4933-9f80-467e84e879cd/ – CharithJ
既然这是作业,到目前为止您尝试过什么? –