0

2 つのテキスト ボックスから 2 つの複数行の入力を取得し、それらを 2 つの配列に入れて比較する簡単なプログラムを作成しようとしています。

配列 1 のエントリ (テキスト ボックス 1 の各行は配列 1 の個別のエントリです) が配列 2 にあるかどうかを確認したい (テキスト ボックス 2 の各行は配列 2 の個別のエントリです)。

次に、結果をテキストボックスに出力します。

例えば:

配列 1「1、2、3、4、6」

配列 2「1、3、5、4」

出力する必要があります:

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

私がこれまでに持っているコードは次のとおりです。

 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";
            }
        }


    }

いずれかのテキストボックスに複数の行がある場合、これは正しく機能しません-理由がわかりません..テスト実行で次のことが起こっています:

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

前もって感謝します

4

4 に答える 4

5

いずれかのテキストボックスに複数の行がある場合、これは正しく機能しません - 誰でも理由を理解できますか?

自分で問題の診断に取り組む必要があります。ループの直前に単純なブレークポイントを配置し、続いて配列を調べると、問題がすぐに見つかると思います。

"\r\n"問題は、代わりに分割する必要があることだと確信しています-現在、最後の行以外のすべての行の最後に'\n'不正があり、結果が台無しになります。\r

プロパティを使用してから分割するのではなく、代わりTextにプロパティを使用できます。Lines

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

編集: Guffa の回答に記載されているように、各反復での置換避けたいと思うでしょう。textBox3.Text個人的には、おそらく create a を使用し、List<string>各反復でそれに追加してから、最後に使用します:

textBox3.Lines = results.ToArray();
于 2012-05-30T19:07:28.023 に答える
1

力ルークを使用:

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

区切り文字に「\r」を追加しました。

于 2012-05-30T19:07:17.107 に答える
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();
于 2012-05-30T19:34:16.753 に答える
1

このコードを試すことができます ( を に変更するだけですint) 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) });

生成するもの:

ここに画像の説明を入力

于 2012-05-30T19:28:32.533 に答える