0

Im trying to write a simple program that will take 2 multi line inputs from 2 textboxes, put them in 2 arrays and compare them.

I want to check if an entry in array 1 (each line of textbox 1 is a separate entry in array 1) is in array 2 (each line of text box 2 is a separate entry in array 2).

then output the results to a textbox.

for example:

Array 1 "one, two, three, four, six"

Array 2 "one, three, five, four"

it should output:

one = found
two = not found
three = found
four = found
six = not found

The code i have so far is as follows:

 private void button1_Click(object sender, EventArgs e)
    {
         textBox3.Text = "";
         string[] New = textBox1.Text.Split('\n');
         string[] Existing = textBox2.Text.Split('\n');


       //for each line in textbox1's array
        foreach (string str in New)
        {

            //if the string is present in textbox2's array
            if (Existing.Contains(str))
            {
                textBox3.Text = "   ##" + textBox3.Text + str + "found";
            }
            /if the string is not present in textbox2's array
            else

            {
                textBox3.Text = "    ##" +textBox3.Text + str + "not found";
            }
        }


    }

This is not working correctly if there is more than one line in the either textbox - i cant figure out why.. the following is happening in test runs:

Array 1 - "One"
Array 2 - "One"
Result = One Found


Array 1 - "One"
Array 2 - "One, Two"
Result = One Not Found


Array 1 - "One, Two"
Array 2 - "One, Two"
Result = One found, Two Found

Array 1 - "One, Two, Three"
Array 2 - "One, Two"
Result - One Found, Two Not Found, Three Not Found

Thanks in advance

2
  • 2
    What do you mean by "not working correctly"? Commented May 30, 2012 at 19:04
  • 1
    Have you tried debugging it and seeing what the contents of textBox1.Text and textBox2.Text are? Commented May 30, 2012 at 19:06

4 Answers 4

5

This is not working correctly if there is more than one line in the either textbox - can anyone figure out why?

You should work on diagnosing problems yourself - I suspect that a simple breakpoint just before the loop, following by examining the arrays, would find the issue immediately.

I'm pretty sure the problem is just that you should be splitting on "\r\n" instead of '\n' - currently you'll end up with a rogue \r at the end of all lines other than the last one, which will mess up the results.

Rather than using the Text property and then splitting it, you could just use the Lines property instead:

string[] newLines = textBox1.Lines;
string[] existingLines = textBox2.Lines;
...

EDIT: As noted in Guffa's answer, you will also want to avoid replacing textBox3.Text on each iteration. Personally I'd probably use create a List<string>, add to it on each iteration, then at the end use:

textBox3.Lines = results.ToArray();
Sign up to request clarification or add additional context in comments.

4 Comments

Exactly. The TextBox control has a Lines property that will give you exactly what you want: an array of individual lines.
@w.brian: Indeed, I was just editing to show that very approach :)
Environment.NewLine can also be used for splitting
Perfect, thanks very much - i had been looking at iterations with loops and all sorts of things, i never thought the actual array generation could be the issue. Thanks again - will mark as answer when it lets me.
1

Use the force, Luke:

char[] delimiterChars = { ' ', ',', '.', ':', '\t', '\n', '\r' };
string[] words = text.Split(delimiterChars);

Added '\r' to delimiters.

2 Comments

Actually I don't think that will help, as written - it'll still leave \r at the end of all lines but the last one... and I don't see anything in the question suggesting that the OP wants to split each line into words.
Ok, it was a general answer. Yours is more specific, but will leave commas and other symbols.
1

You could try this code (just change the int to string):

var a = Enumerable.Range(1, 10);
var b = new[] { 7, 8, 11, 12 };

// mixing the two arrays, since it's a ISet, this will contain unique values
var c = new HashSet<int>(a);
b.ToList().ForEach(x => c.Add(x));

// just project the results, you can iterate through this collection to 
// present the results to the user
var d = c.Select(x => new { Number = x, FoundInA = a.Contains(x), FoundInB = b.Contains(x) });

Which produces:

enter image description here

Comments

1
string[] New = textBox1.Text.Split(',').Select(t => t.Trim()).ToArray();
string[] Existing = textBox2.Text.Split(',').Select(t => t.Trim()).ToArray();
StringBuilder sb = new StringBuilder();
foreach (var str in New)
{
    sb.AppendLine(str + (Existing.Contains(str) ? " found" : " not found"));
}
textBox3.Text = sb.ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.