计算字符并在toolStripStatusLabel中显示

时间:2015-09-04 15:54:48

标签: c# toolstrip

我想计算每次将文本放入富文本框时的字符数。

(如果我输入'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.";
        }

肯定要对这条线做点什么。事实上,它给了我WORD不算字符。

char[] arrCharacter = new char[1] { ' ' };

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我认为您的代码太复杂了:)

我会使用更像的东西:

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.";