-1

I have wrote this code to add text from one richbox to another without the stop words but it is copying all the text from richtext1

     string[] Stopwords = { "a", "about", "actually", "after", "also", "am", "an", "and",
 "any", "are", "as", "at", "be", "because", "but", "by", "could", "do", "each", "either",
 "en", "for", "from", "has", "have", "how","i", "if", "in", "is", "it", "its", "just",
 "of", "or", "so", "some", "such", "that", "the", "their", "these", "thing", "this", "to",
 "too", "very", "was", "we", "well", "what", "when", "where", "who", "will", "with",
 "you", "your"
            };

This the code in the button that should do it

private void button2_Click(object sender, EventArgs e)
        {
            string st = richTextBox1.Text;
            string[] split = st.Split(' ');

            richTextBox2.Text = "";
            int c = 0;
            foreach (string s in split)
            {
                if (!Stopwords.Contains(s))
                {
                    richTextBox2.Text += s + " ";
                }
                else c++;

            }
        }

when I write if (Stopwords.Contains(s)) it prints all stop words in richtext1

here it shows the input and the output are the same

  • 1
    Can you show an example of input/output. For example for inputs like `Now is the time for all good men to come to the aid of the party` or `a stitch in time saves nine`, what would be the output (and why). Note that the second example starts with one of your stop words. – Flydog57 Mar 09 '20 at 22:58
  • When I asked to show inputs and outputs, I meant "what you want as output", not what your program is doing. For what it's worth, unless your text has no punctuation, you will regret using space to delimit words. You need to consider the start of your text, the end of your text and any punctuation that may occur. Then, when you glue things back together, do you really just want to use spaces (i.e., throw away all punctuation) – Flydog57 Mar 09 '20 at 23:05
  • The first words of the left box in your image read "Word > Collaboration compare documents with the legal blackline option". In the middle box there's "Word > Collaboration compare documents legal blackline option", "with" and "the" are clearly gone. So your claim it'd copy all words doesn't hold. – sticky bit Mar 09 '20 at 23:12
  • 2
    As @Flydog57 commented input and output (expected + actual) shown as a text would greatly improve the question. Also please review [mcve] guidance on posting the code in a question - i.e. in case of this question there is no need of rich text control - just hardcoding a value like "The dog" (instead some mysterious value of `richTextBox1.Text`) would greatly simplify code and show problem of "how to do case insensitive `Contains`" much easier. – Alexei Levenkov Mar 09 '20 at 23:13
  • Okay its seems its removing some of the stop words not all like "the" its removed in the first sentence and third sentence but not in the second sentence – Rabih Ibrahim Mar 09 '20 at 23:22

2 Answers2

-1

A simple version of code to do that, using LinQ

    private void button2_Click(object sender, EventArgs e)
    {
        string st = richTextBox1.Text;
        string[] split = st.Split(' ');

        richTextBox2.Text = string.Join(' ', split.Where(x => !Stopwords.Contains(x.ToLower())).ToArray());
    }
Alex - Tin Le
  • 1,982
  • 1
  • 6
  • 11
-1

I would not recommend writing directly to the txt. Have you considered the following:

string st = richTextBox1.Text;
        string[] split = st.Split(' ');

        StringBuilder sb = new StringBuilder();

        int c = 0;
        foreach (string s in split)
        {
            if (!Stopwords.Contains(s))                
                sb.Append($"{s} ");                 
            else 
                c++;
        }

        richTextBox2.Text = sb.ToString().Trim();
Scriptworks
  • 495
  • 1
  • 6
  • 10