Verify string does NOT contain a value other than known values ​​[VB.NET]

I am trying to verify that a string contains only known values. In this case, I need to make sure that it contains only “Shift”, “Control” or “Alt”, but not necessarily all of them. For example, this should be true: "Shift + P", "Shift + Control + H", "Alt + U", but it should not: "Other + P", "Shift + Fake + Y", "Unknown + Shift "+ E" etc.

This is the code I tried to use:

If Not shortcut.Contains("Shift") Or Not shortcut.Contains("Control") Or Not shortcut.Contains("Alt") Then
    MessageBox.Show("Invalid")
End If

I have difficulty wrapping around the right logic for this. I guess there is a logical operator that can do this?

+3
source share
2

, . , , "" (Alt, Control, Shift), . / , "" . .

System.Windows.Forms.Keys . ( , "" ) - - . .

, . , "Shift + Other" , "Shift + F". , , "+" ( , ), , , -, "Shift", "Alt", Control " .

+1

, , , , . #, , .

#:

        string text = "Shift+Control+Alt+Other";
        string[] textSegments = text.Split('+');
        string[] allowedWords = { "Alt", "Shift", "Control" };
        foreach (string t in textSegments)
        {
            bool temp = false;

            foreach (string t2 in allowedWords)
            {
                if (t == t2)
                {
                    temp = true;
                    continue;
                }
            }

            if (!temp)
            {
                MessageBox.Show("Wrong!");
            }
            else
            {
                MessageBox.Show("Right!");
            }


        }

4 MessageBoxes: "Right!" "!" "!" "!"

0

Source: https://habr.com/ru/post/1777272/


All Articles