-2

I'm not sure, if this question is unique, but I couldn't find the answer that I was looking for.

I simply need a C# code that counts how many times a word appear in richTextBox1 and send the result to label1.

Example; label1.text = how many times the word "house" appears in richTextBox1.

I know that I should try first but believe me I tried and I failed. I am new to this so I hope someone can show me.

Regards

alt2090
  • 7
  • 2
  • 2
    If you've tried and failed, show us the non-working code so we can help you find your bug. – adv12 Jan 01 '16 at 23:07

1 Answers1

0

I would solve it using Regular Expressions as follows:

using System.Text.RegularExpressions;

...

string searchstring = ....;
string input = richTextBox1.Text;
int count = Regex.Matches(input, searchstring, RegexOptions.IgnoreCase).Count;

label1.Text = count.ToString();

Do please note, that the code above only works for one single word containing the characters a-z, A-Z, 0-9 and _.


EDIT No.1: If you want to match any exact sequence, you could use the following method:
using System.Text.RegularExpressions;

...

string searchstring = ....;
string input = richTextBox1.Text;

searchstring = Regex.Escape(searchstring);

int count = Regex.Matches(input, searchstring, RegexOptions.IgnoreCase).Count;

label1.Text = count.ToString();

The method above should match any UTF-16 character sequence and count it accordingly.

unknown6656
  • 2,765
  • 2
  • 36
  • 52
  • Thank you Unknown6656 for the help. I did add [using System.Text.RegularExpressions;] at the right place on the top and the rest of the code under private void richTextBox1_TextChanged(object sender, EventArgs e) Is this right? I couldn't make it to work complete. I do get a "0" on label1. Any idea what I did wrong? Maybe you could simply add a search word ''apple'' in the C# code under private void richTextBox1_TextChanged(object sender, EventArgs e) so I can see if I do anything wrong. And again thank you, I really appreciate it. – alt2090 Jan 01 '16 at 23:42
  • @alt2090: Yes, Sir - you did insert the snippet at the correct places inside your code. I will review my code snippet and look for any errors and notify you after I have found your issue :) – unknown6656 Jan 01 '16 at 23:46
  • @alt2090: I tested my code and it worked without any issues. Do you have special characters, line breaks (such as `\n`, `\r`) or spaces etc. in your search string? Or is your code somehow skipping over the upper/lower case of your search string? – unknown6656 Jan 01 '16 at 23:54
  • private void richTextBox1_TextChanged(object sender, EventArgs e) { string searchstring = "\n verdi"; string input = richTextBox1.Text; input = string.Join("", input.Select(c => "\\u" + ((ushort)c).ToString("x4"))); int count = Regex.Matches(input, searchstring).Count; label5.Text = count.ToString(); } – alt2090 Jan 02 '16 at 00:06
  • @alt2090: I found my mistake and I changed my code accordingly. Please [click here to see my edits](http://stackoverflow.com/posts/34561025/revisions). – unknown6656 Jan 02 '16 at 00:23
  • @alt2090: Do please remember to close this question (by ticking the correct answer with the green tick-mark), if this solved your issue – unknown6656 Jan 02 '16 at 10:00