Click here to Skip to main content
16,005,826 members
Please Sign up or sign in to vote.
2.41/5 (3 votes)
See more:
i have two richTextBoxes, one containing list of words and other whole text. i want to compare both textboxes and if word from list_of_word textbox found in whole_Text text box than copy that word to 3rd textbox.

Any help would be appreciated! thanks in advance friends. :)
Posted
Comments
Dave Kreskowiak 20-Jan-15 12:15pm    
And what are you having a problem with?

No, we're not doing your homework for you.
Afzaal Ahmad Zeeshan 20-Jan-15 12:17pm    
You can try to get the IndexOf each of the word in your first RichTextBox and then check for its value in the second RichTextBox, if the result is positive then the word exists. Append it to the third one.

Tip: Why are you even using the RichTextBox for such process? You should try ignoring using it, and try using TextBox itself.
ZurdoDev 20-Jan-15 12:27pm    
Where are you stuck? The String object has a method Contains() that would be really easy to use here.
Sergey Alexandrovich Kryukov 20-Jan-15 12:44pm    
Can you define "compare"? It is not as trivial as you may think.
And why RichTextBox?
—SA
BillWoodruff 20-Jan-15 12:46pm    
WinForms ? , WPF ? Upper-case treated same as lower-case ?

Try this: (assume you want to show the difference in a third RichTextBox named 'richTextBox3):
C#
private char[] splitChars = new char[] 
{   
    ' ',    // space
    ',',    // comma
    ';',    // semi-colon
    ':',    // colon
    '\t',   // tab
    '\n',   // newline
    '\r',   // return
    '-',    // hyphen
    '!',    // exclamation 
    '/',    // slash
    '\\'    // backslash
};

private void FindDifference_Click()
{
    string[] words1 = richTextBox1.Text.ToLower().Split(splitChars, StringSplitOptions.RemoveEmptyEntries);

    string[] words2 = richTextBox2.Text.ToLower().Split(splitChars, StringSplitOptions.RemoveEmptyEntries);

    var difference = words1.Except(words2);

    richTextBox3.Clear();

    if (difference.Count() == 0)
    {
        richTextBox3.Text = "No difference.";
        return;
    }

    StringBuilder sb = new StringBuilder();

    foreach(var wrd in difference)
    {
       sb.Append(wrd);
       sb.AppendLine();
    }

    richTextBox3.Text = sb.ToString();
}
For production code I would want to use a compiled RegEx rather than String.Split.
 
Share this answer
 
Comments
jaket-cp 22-Jan-15 11:12am    
Got my 5.
As my solution only gives a linq example of how to compare the strings lists.
Also referred to your solution multiple times :)
the op got there in the end.

Good tip on Compiled RegEx, didn't know about those :)
Asad_Iqbal 14-Apr-15 12:37pm    
Thanks Aloooooooot :)
Asad_Iqbal 14-Apr-15 12:38pm    
got my 5 too :)
Linq can be used to help in comparing the strings.
C#
//test data setup (www.lipsum.com)
String[] ListOfWords = {"Duis","urna","enim","rhoncus","sit","amet","dignissim","ut","sollicitudin","at"};
String WholeText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lacinia ex euismod lectus convallis lobortis. Integer vitae faucibus lectus, et molestie enim. Fusce condimentum sapien et felis iaculis volutpat. Nam vel mauris id velit consectetur pulvinar at et tellus. Fusce id lacus pretium, vehicula enim a, condimentum urna. Nam id erat est. Ut eu risus ultricies, dapibus leo quis, sollicitudin velit. Phasellus quis nulla ligula. Nam rhoncus leo vitae magna aliquet vulputate. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum faucibus ante nisi, a iaculis ipsum consectetur quis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Ut sit amet lobortis eros. Nullam at lectus sit amet nibh congue tempor. Donec lobortis dapibus neque, at dapibus tortor vulputate sit amet.";

//populate FindWord with match
//note: comma, fullstops and the like are not taken into account when splitting WholeText into words
var FindWord = (from word in ListOfWords
	where WholeText.Split(' ').Contains(word)
//	where Array.Exists(WholeText.Split(' '), s => {return (s.Equals(word))?true:false;})
	select word
);

//OR
var FindWord = WholeText.Split(' ').Intersect(ListOfWords);

//get results
foreach (var s in FindWord){
	Console.WriteLine(s);
}

Enumerable.Intersect<tsource> Method: http://msdn.microsoft.com/en-us/library/vstudio/bb460136(v=vs.100).aspx[^]
 
Share this answer
 
Comments
Asad_Iqbal 21-Jan-15 8:23am    
String[] ListOfWords = {targetTextBox.Text};
String WholeText = richTextBox2.Text;
//populate FindWord with match
//note: comma, fullstops and the like are not taken into account when splitting WholeText into words
var FindWord = (from word in ListOfWords
where WholeText.Split(' ').Contains(word)
// where Array.Exists(WholeText.Split(' '), s => {return (s.Equals(word))?true:false;})
select word
);

//OR
FindWord = WholeText.Split(' ').Intersect(ListOfWords);

//get results
foreach (var s in FindWord)
{
textBox1.Text = s;
}

i tried this i didn't get any output.. where m i doing wrong???
jaket-cp 21-Jan-15 8:49am    
Initially get the targetTextBox.Text value into a string array
Read the following to get up to speed on that one:
https://msdn.microsoft.com/en-gb/library/aa288453(v=vs.71).aspx
http://www.dotnetperls.com/string-array

Now when that is done, the value in richTextBox2.Text (whole text) needs to be split into separate words.
This can be done by using split(' '), but as I have noted this will only split/separate the words in the whole text by the ' ' (space).

Now when both are in a array string format the can be compared.
Populate FindWord with one of the two methods specified.

When FindWord is populated, loop through to populate textBox1.Text.
for example:
textBox1.Text = textBox1.Text + s

Hope that helps out.

Asad_Iqbal 21-Jan-15 9:02am    
thanks for helping me out and clearing my concept regarding this..

i use below code..

String[] ListOfWords = {richTextBox2.Text};
String WholeText = targetListBox.Text;
//populate FindWord with match
//note: comma, fullstops and the like are not taken into account when splitting WholeText into words
var FindWord = (from word in ListOfWords
where WholeText.Split(' ').Contains(word)
//where Array.Exists(WholeText.Split(' '), s => {return (s.Equals(word))?true:false;})
select word);

textBox1.Text = textBox1.Text + FindWord.ToString();

and got this message in a textbox.

System.Linq.Enumerable+WhereArrayIterator`1[System.String]

where did i go wrong bro..
jaket-cp 21-Jan-15 10:29am    
okay the line
String[] ListOfWords = {richTextBox2.Text};
is not probably not population with a list of words.
Have a look at solution 2 to see a way of populating String[] ListOfWords

The error is probably due to String[] ListOfWords not being populated as expected.

FindWord.ToString(); will probably not work.

Loop through the values in FindWord and create a string or stringbuilder and then populate textBox1, similar to how in solution 2.


Asad_Iqbal 21-Jan-15 12:04pm    
String[] ListOfWords = {richTextBox2.Text};

this work fine as i use debugger to check weather it is populated or not... and i tried solution 2 and problem in solution 2 is that it never enter the foreach loop..

FindWord = WholeText.Split(' ').Intersect(ListOfWords);

//get results
foreach (var s in FindWord)
{
textBox1.Text = s;
}
 
Share this answer
 
Comments
BillWoodruff 20-Jan-15 12:45pm    
If the Op is using WPF.
Sergey Alexandrovich Kryukov 20-Jan-15 12:47pm    
Essentially, it solves the major part of the problem. 5ed. I'm not sure inquirer understand clearly what "compare" means; it may be a problem, but let him define it first.
—SA
Abhinav S 20-Jan-15 16:14pm    
Thank you SA.
Asad_Iqbal 21-Jan-15 8:27am    
by means of comparing is: compairing two richtextbox's text and two find out the matching word and displaying it in 3rd TextBox
C#
            var regex = new Regex(richTextBox2.Text);
            List<string> matchingLines = new List<string>();
            foreach (var richtextItem in targetTextBox.ToString())// loop every item in your targetListBox
            {
                var texts = targetextBox.Text.ToString().Split(' ');
                foreach (var t in texts)
                {
                    if (regex.IsMatch(t))
                    {
                        matchingLines.Add(t.Trim());
                    }
                }
                break;
            }
            textBox1.Lines = matchingLines.ToArray();


</string></string>



so far this is what i did... but not getting any output..
 
Share this answer
 
v2
Comments
BillWoodruff 21-Jan-15 4:26am    
You have changed your question with this: your question asks about comparing Text in two RichTextBoxes: this example shows you using a ListBox. 'targetListbox.ToString makes no sense whatsoever if 'targetListBox is a real ListBox Control.

This should not be posted as a "solution:" your original question should be edited to include any changes, or updates.
Asad_Iqbal 21-Jan-15 7:44am    
sorry it was a writing mistake. m still working with richtextboxes
BillWoodruff 21-Jan-15 9:51am    
Don't worry you are in the company of people who make mistakes all the time :) The important thing is to edit your original post to keep it up-to-date.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900