计算一个字符并显示在toolStripStatusLabel中
本文关键字:显示 toolStripStatusLabel 字符 一个 计算 | 更新日期: 2023-09-27 18:11:09
我想在每次将文本放入富文本框时计算有多少字符。
(如果我输入'Hello there'(它应该显示"10个字符…"而不是"2个字符…")
private void rtbText_TextChanged(object sender, EventArgs e)
{
char[] arrCharacter = new char[1] { ' ' };
int countChar = rtbText.Text.Split(arrCharacter).Length;
char[] arrVowels = new char[5] { 'a', 'e', 'i', 'o', 'u' };
int countVowels = rtbText.Text.Split(arrVowels).Length;
toolStripStatusLabel1.Text = countChar + " characters, of which " + countVowels + " are vowels.";
}
肯定和这一行有关系。事实上,它给我的是字数而不是字符。
char[] arrCharacter = new char[1] { ' ' };
谢谢你的帮助!
我觉得你的代码太复杂了:)
我更喜欢这样写:
var vowels = new char[]{ 'a', 'e', 'i', 'o', 'u' };
var vowelCount = rtbText.Text.Count(c => vowels.Contains(c));
var characterCount = rtbText.Text.Length;
toolStripStatusLabel1.Text = characterCount + " characters, of which "
+ vowelCount + " are vowels.";