Home > Mobile >  Removing a specific string in the array using a List Array
Removing a specific string in the array using a List Array

Time:12-06

I am creating a simple Bingo game.

I stored the different number in a String Array

    String[] number = new[] { "" };
    
    if (lblLetter.Text == "B")
    {
        //number = new[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" };
    }
    if (lblLetter.Text == "I")
    {
        //number = new[] { "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30" };
    }
    if (lblLetter.Text == "N")
    {
        //number = new[] { "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45" };
    }
    if (lblLetter.Text == "G")
    {
        //number = new[] { "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60" };
    }
    if (lblLetter.Text == "O")
    {
        number = new[] { "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75" };
    }

I used a List Array to store the numbers that was already selected on the bingo thus should be removed on the String Array

public List<string> exceptions = new List<string>();

Appending the label Value to it

exceptions.Add(lblNumber.Text);

Now I want to remove specific Items on String Array that is found on the exceptions List String

I'm confused on how to do this, Any help will be appreciated.

CodePudding user response:

You can use Except.

number = number.Except(exceptions).ToArray();

CodePudding user response:

Here is your solution


String[] number = new[] { "1", "2", "3", "4", "5" };

            List<string> exceptions = new();
            exceptions.Add("1");
            exceptions.Add("2");
            exceptions.Add("22");
            exceptions.Add("33");
            exceptions.Add("44");
            exceptions.Add("60");

            var result = number.Where(x => !exceptions.Contains(x)).ToList();

  • Related