[英]How to count characters in c# from a richtextbox
好吧,基本上我在这里要完成的是计算richtextbox中的所有字符并显示它们中的哪些是元音,它将不断更新并显示工具条中的字符/元音数量。 相当简单的代码,但在foreach循环和textchanged中似乎有一些错误。 会很感激一些反馈请:)
private void Form1_Load(object sender, EventArgs e)
{
int vowels;
int characters;
foreach(int char in rtbDoc)
{
characters+=1;
}
if (rtbDoc.Text == "a".ToLower() || rtbDoc.Text == "e".ToLower()
|| rtbDoc.Text == "i".ToLower() || rtbDoc.Text == "o".ToLower()
|| rtbDoc.Text == "u".ToLower())
{
vowels += 1;
}
toolStripStatusLabel1.TextChanged = characters +
"Characters, of which " + vowels + " are vowels";
}
这是Linq方法
char[] Vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };
int CharacterCount = rtbDoc.Text.Length;
int VowelCount = rtbDoc.Text.Count(x => Vowels.Any(y => char.ToLower(x) == y));
首先我建议提取一个方法 :
private static bool IsVowel(Char value) {
//TODO: is 'y' a vowel?
return
value == 'a' || value == 'i' || value == 'e' || value == 'o' || value == 'u' ||
value == 'A' || value == 'I' || value == 'E' || value == 'O' || value == 'U';
}
然后使用Linq :
//TODO: do you really want to compute vowels count on Form Load? Not on rtbDoc.TextChanged?
private void Form1_Load(object sender, EventArgs e) {
String text = rtbDoc.Text;
// Finally, formatting which is more readable
//DONE: you've probably want to assign to Text, not to TextChanged
toolStripStatusLabel1.Text =
String.Format("{0} Characters, of which {1} are vowels.",
text.Length,
text.Count(c => IsVowel(c)));
}
如果它是元音,你必须检查每个字符
private void Form1_Load(object sender, EventArgs e)
{
char[] vowelSet = { 'a', 'e', 'i', 'o', 'u' };
int vowels = 0;
int characters = 0;
foreach(char c in rtbDoc.Text.ToLower())
{
characters+=1;
if (vowelSet.Contains(c))
{
vowels += 1;
}
}
toolStripStatusLabel1.TextChanged = characters +
"Characters, of which " + vowels + " are vowels";
}
要在更改后更新标签,您必须使用RichTextBox的TextChanged事件
如果您希望每次更改时都更新代码,那么您的代码应该是这样的
// you code should be inside the TextChanged event for the richtextbox to get called everytime the text is modified
private void rtbDoc_TextChanged(object sender, EventArgs e) {
int vowels = 0; // you should initialize your variables
int characters = 0;// you should initialize your variables
foreach (char c in rtbDoc.Text) { // you can't name a variable " char ", and you should choose rtbDoc.Text
characters += 1;
if (c.ToString().ToLower() == "a" || c.ToString().ToLower() == "e" // ToLower() function should be on the input
|| c.ToString().ToLower() == "i" || c.ToString().ToLower() == "o" // you should check the character not the whole text
|| c.ToString().ToLower() == "u") {
vowels += 1;
}
}// the if part should be inside the foreach loop to check every character
toolStripStatusLabel1.Text = characters + // you should use toolStripStatusLabel1.Text
"Characters, of which " + vowels + " are vowels";
}
你快到了。
您的if
语句表示如果框中的文本是“a”(或任何其他元音字符),请添加一个。 否则,你什么都不做。 例如,输入“aeiou”。 你的if语句不会添加一个元音,然后你订阅textchanged就完成了。 目前,您没有循环RichTextBox中的文本。 由fubo概述的Linq方法工作并且更加清洁,但是与你已经拥有的东西相比,你应该做的是:
var characters = rtbDoc.Text.Length;
foreach(var char in rtb.Text.ToLower())
if(IsVowel(char))
vowel+=1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.