简体   繁体   中英

How do you remove string array result until you have zero results?

I have a form Form1 with an array in it:

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

Where value2 is a text document like this:

x
y
z

I then have another form Form2 that collects randomly one of the arrays to a string => person . This form also has a return button that returns to Form1 . Person is a public static string .

I am trying to work out a way that when you return to Form1 You can not use the same random value person again.


I have tried:

lines = lines.Where(s => s != person).ToArray(); on the submit to Form2 Click. But that doesn't seem to work.


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)
        {

        }

    }
}

The code you have

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

... actually works.

Now, if it's not working, that means that your string person does not match any item in the array . Chances are either there are extra spaces, or more likely, the capitalization isn't the same.

Note that if person is "X" and lines is { "x", "y", "x" } then your code won't match any item in lines .

Try to normalize the capitalization:

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

Finally, note that if the file you're reading from has a newline at the end, you'll get an empty entry in your lines array. Then naturally if that empty string gets assigned to person , this code won't match that to anything in the array and won't remove any item.

To avoid this, use StringSplitOptions.RemoveEmptyEntries when doing your string.Split() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM