2016-04-15 11 views
3

Ok, im Grunde versuche ich hier, alle Zeichen in der richtextbox zu zählen und zu zeigen, welche von ihnen die Vokale sind und sie werden ständig aktualisieren und die Anzahl der anzeigen Zeichen/Vokale in der Werkzeugleiste. Ziemlich einfacher Code, aber es scheint einige Fehler in der foreach-Schleife und der Textänderung zu geben. Wäre dankbar bitte ein FeedbackWie man Zeichen in C# aus einer richtextbox zählt

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

Sie führen einen .ToLower() auf dem statischen Wert aus. Dies sollte auf dem rtbDoc.Text erfolgen. – UnhandledExcepSean

+1

Sollte nicht das 'if' in der Schleife sein und auch das char überprüfen? – Magnus

+0

Ihr 'IF' befindet sich außerhalb der Foreach, Ihre Foreach verwendet das Steuerelement und nicht RtbDoc.Text. – Prix

Antwort

7

:) Hier ist ein Linq Ansatz

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

Damit es mit Nicht-ASCII-Zeichen funktioniert: http://stackoverflow.com/a/35088029/1207195 (und jede andere Sprache was ist nicht en-US). Randnotiz: Sie können direkt .Count (x => ...) anstelle von .Where (x => ...) verwenden. Count() –

+1

Die Lesbarkeit für die Kurzform wird geopfert - 'int VowelCount = text.ToLower(). Count (Vowels.Contains); ' – stuartd

+0

Und für alle anderen, die zum Beispiel über die türkische Sprache nachdenken: ** Benutze niemals ToLower() ** in deinem Code. 99,9% ist es die falsche Wahl. Benutze immer einen kulturbewussten StringComparer. –

2

Zuerst schlage ich vor, ein Verfahren Extrahieren:

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

Dann mit 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))); 
} 
+0

@fubo: Nun, 'text.Count (IsVowel)' ist * kürzer *, aber, IMHO, 'Count (c => IsVowel (c))' ist besser lesbar. –

+0

'toolStripStatusLabel1.TextChanged' – Prix

+1

@Prix: Danke! Kopieren + Einfügen von einer Frage ist eine * schlechte * Sache. –

-1

Der Code sollte so sein, wenn Sie es jedes Mal, es ändert sich

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

Sie sind fast da aktualisieren möchten.

Ihre Anweisung if besagt, dass, wenn der Text in der Box "a" (oder ein anderes Vokalzeichen) ist, eins hinzugefügt wird. Ansonsten machst du nichts. Nehmen Sie zum Beispiel die Eingabe "aeiou". Ihre if-Anweisung fügt keine Vokale hinzu, dann abonnieren Sie Textänderungen und Sie sind fertig. Derzeit werden Sie den Text in der RichTextBox nicht durchlaufen. Die Linq Ansatz von fubo Werke skizziert und ist viel sauberer, aber geht mit dem, was Sie bereits haben, was Sie wirklich tun sollten, ist:

var characters = rtbDoc.Text.Length; 

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

Ich habe versucht, diese 1 zu verwenden, da es einfach und ziemlich einfach aussah, aber es hat leider nicht geklappt. Aber danke für das Feedback :) –

0

Sie haben jedes Zeichen zu überprüfen, ob es ein Vokal ist

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

Um das Etikett nach Änderungen zu aktualisieren, müssen Sie das TextChanged-Ereignis der RichTextBox verwenden.

Verwandte Themen