-2

I would like to be able to:

  • Check each character in a string (userInput)
  • If second string contains this character, remove it from the string

Therefore: Take every character in a string and validate that these characters are all in the second string.

For example, if the second string was: "abcdefg" and the user input was "acf", the code should remove the characters "a" "c" and "f" from "abcdefg", therefore checking that all input values are valid (in the other string). This way, duplicates are accounted for.

The code I have currently:

foreach (char letter in userInput)
        {

            if (letters.Contains(letter))
            {
                //Here would be the code for removing the character from the string
                Console.ReadKey();
            }

            else
            {
                Console.WriteLine("Cheater!");
                Console.ReadKey();
                Environment.Exit(0);
            }
        }

string letters = contains 7 letters string userInput = used to input letters up to 7.

1 Answers1

-1
        string str = "abcdef";
        string rem = "acf";

        foreach (char cc in rem)
        {
            str = str.Replace(cc.ToString(), "");
        }
        Console.WriteLine(str);
Mortaliar
  • 58
  • 4
  • I've attempted to utilize this in my code however it seems to only remove the first letter that is input, such as if it was acf it would remove a from abcdef, but not c or f. – Joe Podmore Feb 08 '18 at 14:00
  • Did you try to debug this sample and see what is going on inside the loop? – Mortaliar Feb 08 '18 at 14:03
  • Thanks, works great :), just had an issue with my ReadKey making it display once and I didn't press it multiple times to find out that it did infact remove multiple, I just had to continue pressing enter due to the loop of ReadKeys. This was perfect for what I wanted so thank you very much, I really don't think this is a duplicate post due to handling characters and strings rather than two strings. – Joe Podmore Feb 08 '18 at 14:14