0

The user pastes a junk of text into a textbox, then he enters specific in another textbox a word like "Good". Then he/she hits a button which does a two things:

  • counts the number of "Good" in that string and then displays it in a label like .. "4 TIMES"
  • replaces all of them with another word that the user entered

This is what I have so far.

 private void button2_Click(object sender, EventArgs e)
 {
        string Text = TextBox.Text;
        Text.Contains("Good");
        int position = Text.IndexOf("Good");
        int i = 0;

        while(position != -1 )
        {
            MessageBox.Show(position.ToString());
            position = Text.IndexOf("Good", position);
            i++;
        }

        lbl.Text = i.ToString();
}
Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
  • https://stackoverflow.com/questions/3016522/count-the-number-of-times-a-string-appears-within-a-string Has some great answers. – Marker May 09 '18 at 20:45
  • @Marker - You can just flag the post as a duplicate (as I have done, using the link you've provided). – Broots Waymb May 09 '18 at 20:47
  • @BrootsWaymb - `@Marker` does not have enough rep to do so (yet). – Igor May 09 '18 at 20:48
  • @Igor - Does that not require only 15 rep? Or am I thinking of some other privilege? – Broots Waymb May 09 '18 at 20:51
  • https://stackoverflow.com/questions/541954/how-would-you-count-occurrences-of-a-string-actually-a-char-within-a-string also has some good info – Sorceri May 09 '18 at 20:52
  • @BrootsWaymb - That requires a rep of 3,000. See [Help Center > Privileges](https://stackoverflow.com/help/privileges) – Igor May 09 '18 at 20:52
  • 2
    @Igor - I think we have a miscommunication. I just meant flagging the post (hence when I said "as I have done"), not close votes. – Broots Waymb May 09 '18 at 20:54
  • @BrootsWaymb - You are correct, I misread your comment. – Igor May 09 '18 at 20:57

0 Answers0