如果结果为零,如何删除字符串数组结果?

时间:2014-02-12 04:18:15

标签: c#

我的表单Form1中有一个数组:

string [] lines = value2.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

其中value2是这样的文本文档:

x
y
z

然后我有另一种形式Form2,它将一个数组随机收集到一个字符串=> person。 此表单还有一个返回Form1的返回按钮。 人是public static string

我想找到一种方法,当你返回Form1时,你不能再使用相同的随机值person


我试过了:

提交到lines = lines.Where(s => s != person).ToArray();

Form2点击。但这似乎不起作用。


Form2

namespace moving_random_to_another_page
{
    public partial class Form2 : Form
    {

        string person = Form1.person;
        Form opener;

        public Form2(Form parentForm)
        {
            InitializeComponent();
            opener = parentForm;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            opener.Show();
            this.Hide();
        }
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

            this.label1.Text = person;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

    }
}

Form1

namespace moving_random_to_another_page
{


    public partial class Form1 : Form
    {
        public static int array_lenth2;
        public static int array_lenth;
        public static string[] lines2;
        public static string[] lines;
        public static string participants;
        public static string prizes;

        public static string person;
        public static string prize;

        public Form1()
        {
            InitializeComponent();
        }
        static string ConvertStringArrayToString(string[] array)
        {
            //
            // Concatenate all the elements into a StringBuilder.
            //
            StringBuilder builder = new StringBuilder();
            foreach (string value in array)
            {
                builder.Append(value);
                builder.Append("\r\n");
            }
            return builder.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                string file = openFileDialog1.FileName;

                string value = File.ReadAllText(file);

                this.textBox1.ReadOnly = true;
                // Use ToCharArray to convert string to array.
                lines = value.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                array_lenth = lines.Length;
                Random rnd = new Random();
                int month = rnd.Next(0, array_lenth);
                participants = ConvertStringArrayToString(lines);
                person = lines[month];
                DialogResult dialogResult = MessageBox.Show("Are these all the participants? \r\n" + participants, "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                 if (dialogResult == DialogResult.Yes)
                 {

                 }
                 else
                 {
                     array_lenth = 0;
                     return;
                 }
                 this.textBox1.Text = file;

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                string file = openFileDialog1.FileName;

                string text = File.ReadAllText(file);

                string value2 = text;

                this.textBox2.ReadOnly = true;

                // Use ToCharArray to convert string to array.
                lines2 = value2.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                array_lenth2 = lines2.Length;
                Random rnd = new Random();
                int month = rnd.Next(0, array_lenth2);

                prizes = ConvertStringArrayToString(lines2);
                DialogResult dialogResult = MessageBox.Show("Are these all the prizes? \r\n" + prizes, "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                }
                else
                {
                    array_lenth2 = 0;

                }
                this.textBox2.Text = file;
            }
        }

        private void button3_Click(object sender, EventArgs e)
           {

            if ( array_lenth2 == array_lenth && array_lenth > 1 && array_lenth2 > 1)
            { 
                    lines = lines.Where(s => s.ToLower() != person.ToLower()).ToArray();
                    Form2 x = new Form2(this);
                    x.Show();
                    this.Hide();

            }
            else
            {
                MessageBox.Show("Error with entries. Please try again!","",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

    }
}

1 个答案:

答案 0 :(得分:0)

您拥有的代码

lines = lines.Where(s => s != person).ToArray();

......实际上有效。

现在,如果它不起作用,则表示您的字符串personarray中的任何项都不匹配。机会是有额外的空间,或者更有可能,大写不一样。

请注意,如果person"X"且行为{ "x", "y", "x" },那么您的代码将与lines中的任何项目都不匹配。

尝试规范化大写:

lines = lines.Where(s => s.ToLower() != person.ToLower()).ToArray();

最后请注意,如果您正在阅读的文件末尾有换行符,则会在行数组中输入一个空条目。然后,如果将空字符串分配给person,则此代码将与数组中的任何内容不匹配,并且不会删除任何项目。

要避免这种情况,请在执行StringSplitOptions.RemoveEmptyEntries时使用string.Split()

lines = value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);