Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 249913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:28:02+00:00 2026-05-11T21:28:02+00:00

I got richtextBox control in form and a text file. I am getting text

  • 0

I got richtextBox control in form and a text file. I am getting text file to array and getting richtextbox1.text to other array than compare it and count words matching.

But for example there are two “name” word in text file and three “and” word in richtextbox. So if there is two same word in text file in richtextbox it cant be 3 or higher after 2, it must be wrong word so it must not be counted. But HashSet is counting unique values only not looking for duplicates in text file. I want to compare every word in text file with words in RichTextBox.

My code is here:

        StreamReader sr = new StreamReader("c:\\test.txt",Encoding.Default);
        string[] word = sr.ReadLine().ToLower().Split(' ');
        sr.Close();
        string[] word2 = richTextBox1.Text.ToLower().Split(' ');
        var set1 = new HashSet<string>(word);
        var set2 = new HashSet<string>(word2);
        set1.IntersectWith(set2);

        MessageBox.Show(set1.Count.ToString());
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:28:02+00:00Added an answer on May 11, 2026 at 9:28 pm

    Inferring that you want:

    file:

    foo
    foo
    foo
    bar
    

    text box:

    foo
    foo
    bar
    bar
    

    to result in ‘3’ (2 foos and one bar)

    Dictionary<string,int> fileCounts = new Dictionary<string, int>();
    using (var sr = new StreamReader("c:\\test.txt",Encoding.Default))
    {
        foreach (var word in sr.ReadLine().ToLower().Split(' '))
        {
            int c = 0;
            if (fileCounts.TryGetValue(word, out c))
            {
                fileCounts[word] = c + 1;
            }
            else
            {
                fileCounts.Add(word, 1);
            }                   
        }
    }
    int total = 0;
    foreach (var word in richTextBox1.Text.ToLower().Split(' '))
    {
        int c = 0;
        if (fileCounts.TryGetValue(word, out c))
        {
            total++;
            if (c - 1 > 0)
               fileCounts[word] = c - 1;                
            else
                fileCounts.Remove(word);
        }
    }
    MessageBox.Show(total.ToString());
    

    Note that this is destructively modifying the read dictionary, you can avoid this (so only have to read the dictionary once) buy simply counting the rich text box in the same way and then taking the Min of the individual counts and summing them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any light version of richtextbox control? I've got to create like 300+
I've got a WinForms RichTextBox in my application. When I enter the Chinese text
I've got a windows form object that contains 3 objects, a treeview, a richtextbox,
Am using RichTextBox in a C#/Winforms application. Am showing some text in this control
I got a webserver with a running application. There's a webpage with a form:
I have details view control in my asp.net web form, which on of its
I'm trying to add Intellisense to C# code editor based on the richtextbox control.
I've got a simple winform. In it has a single TextBox control. In that,
I've got a richtextbox, that I plan on saving to a database, which can
Is there any way, to save formatted in database from richtextbox? I've got richtextbox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.