计算一个字符并在 toolStripStatusLabel 中显示
Counting a character and display in toolStripStatusLabel
每次我将文本放入富文本框时,我想统计一下有多少个字符。
(如果我输入 '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.";
每次我将文本放入富文本框时,我想统计一下有多少个字符。
(如果我输入 '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.";