2016-12-13 5 views
-3

richtextboxに読み込まれたファイルに何人の女性と男性が含まれているのかを調べる必要があるプログラムがありますが、その方法はファイルではわかりません名前、性別、特定の仕事を持っています。私は、例えばRichTextboxの特定の単語を集計する方法

15の異なる人々の間をカウントする必要があります。「ドナ、女性、人事。」、

これは私がこれまで持っているものです。

private void Form1_Load(object sender, EventArgs e) 
{ 
    StreamReader sr; 
    richTextBox1.Clear(); 
    sr = new StreamReader("MOCK_DATA.txt"); 
    string data; 
    while (!sr.EndOfStream) 
    { 
     data = sr.ReadLine(); 
     richTextBox1.AppendText(data + "\n"); 
    } 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    string[] data = richTextBox1.Text.Split(','); 
    for (int n = 0; n < data.Length; n++) 
    { 
     if (data[n] == richTextBox1.Text) 
      n++; 

答えて

0

からプレーンテキストを取得するには(this articleから盗まれた)リッチテキストボックス:

string StringFromRichTextBox(RichTextBox rtb) 
{ 
    TextRange textRange = new TextRange(
     // TextPointer to the start of content in the RichTextBox. 
     rtb.Document.ContentStart, 
     // TextPointer to the end of content in the RichTextBox. 
     rtb.Document.ContentEnd 
    ); 

    // The Text property on a TextRange object returns a string 
    // representing the plain text content of the TextRange. 
    return textRange.Text; 
} 

基本的なワードカウントルーチン:

int CountWord(string textToSearch, string word) 
{ 
    int count = 0; 
    int i = textToSearch.IndexOf(word); 
    while (i != -1) 
    { 
     count++; 
     i = textToSearch.IndexOf(word, i+1); 
    } 
    return count; 
} 

一緒にそれを置く:

var plainText = StringFromRichTextBox(richTextBox1); 
var countOfMale = CountWord(plainText, "Male"); 
var countOfFemale = CountWord(plainText, "Female"); 
+0

私はあなたが(タグにそれを追加)のWinFormsを使用していることを指定しなかった@LeonardoKafuri私はこのコード –

+0

で特定の単語をカウントする方法を得ることはありません、あなたはWPFの答えを持っています – Slai

+0

@Slai私はwinformsを使用しています –

関連する問題