简体   繁体   中英

How to count characters in c# from a richtextbox

Ok so basically what I am trying to accomplish here is to count all of the characters in the richtextbox and show which of them are the vowels and it will constantly update and show the number of chars/vowels in the toolstrip bar. Pretty straightforward code but there seems to be some errors in the foreach loop and the textchanged. Would appreciate some feedback please :)

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

Here is a Linq approach

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));

First I suggest extracting a method :

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';
}

Then using 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)));
}

You have to check each char if it is a vowel

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

To Update the label after changes, you have to use TextChanged event of the RichTextBox

Your code should be this way if you want it to update every time it changes

 // 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";
    }

You're almost there.

Your if statement is saying that if the text in the box IS "a" (or any other vowel character), add one. Otherwise, you don't do anything. Take for example, the input "aeiou". Your if statement will not add one to vowels, then you subscribe to textchanged and you're done. Currently, you aren't looping over the text in the RichTextBox. The Linq approach outlined by fubo works and is much cleaner but going with what you already have what you really should do is:

var characters = rtbDoc.Text.Length;

foreach(var char in rtb.Text.ToLower())
    if(IsVowel(char)) 
         vowel+=1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM