验证字符串不包含已知值之外的值 [VB.NET]
我正在尝试验证字符串是否只包含已知值。在这种情况下,我需要确保它仅包含“Shift”、“Control”或“Alt”,但不一定包含所有这些。例如,这些应该是 true:“Shift + P”、“Shift + Control + H”、“Alt + U;但这些不应该是:“Other + P”、“Shift + Fake + Y”、“Unknown + Shift” + E" 等。
这是我尝试使用的代码:
If Not shortcut.Contains("Shift") Or Not shortcut.Contains("Control") Or Not shortcut.Contains("Alt") Then
MessageBox.Show("Invalid")
End If
我很难理解执行此操作所需的逻辑。我假设有一个逻辑运算符可以执行此操作?
I am trying to verify that a string contains nothing but known values. In this case, I need to make sure it contains only "Shift", "Control", or "Alt", but not necessarily all of those. For example, these should be true: "Shift + P", "Shift + Control + H", "Alt + U; but these 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'm having difficulty wrapping my head around the needed logic to do this. I'm assuming there's a logic operator that can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您不应该将字符串用于此目的。您需要一种可以表示热键组合的数据类型,该组合由一个“普通”键和一组修饰符(Alt、Control、Shift)组成,每个修饰符都可以打开或关闭。开/关修饰符可以由带有标志的枚举来表示,并且“正常”键可以由单独的枚举来表示。这两个枚举都可以包含在一个类中。
System.Windows。 Forms.Keys 枚举可以用作两个枚举。您可以存储两个数值(一个用于修饰符,一个用于“正常”键) - 底层枚举值 - 并且它们将代表组合。无需存储字符串。
如果您确实使用字符串来实现此目的,则需要更好地定义约束。您的规则没有指定“Shift + Other”如何无效,但“Shift + F”无效。无论如何,解决这个问题的一种方法是用“+”分隔字符串(假设这始终是分隔符),然后将每个部分与有效值列表进行比较,该列表显然包含“Shift”、“Alt”、“控制”和所有单个字母。
I believe you should not use strings for this purpose. You need a data type that can represent a hotkey combination, which is comprised of a "normal" key and a set of modifiers (Alt, Control, Shift), each of which can either be on or off. The on/off modifiers can be represented by an enum with flags, and the "normal" key can be represented by a separate enum. Both of the enums can be contained within a class.
The System.Windows.Forms.Keys enumeration can be used as both enums. You can store two numeric values (one for the modifiers, one for the "normal" key) - the underlying enum values - and they will represent the combination. No need to store strings.
If you do use strings for this purpose, you need to define your constraints better. Your rules do not specify how "Shift + Other" is invalid, but "Shift + F" is. A way to go about this, anyway, is to separate the string by " + " (assuming this is always the separator) and then compare each part to the list of valid values, which apparently contains "Shift", "Alt", "Control" and all single letters.
我认为将字符串分割成段,然后迭代单词列表,然后与应该存在的单词进行比较会更容易。这是用 C# 编写的,但我想你可以弄清楚。
C#:
输出将是 4 个消息框,显示:“对!” “正确的!” “正确的!” “错误的!”
I think it would be easier to split the string into segments and then iterate through the list of words and then comparing to what words should exist. This is in C# but I guess you could figure it out.
C#:
The output will be 4 MessageBoxes displaying: "Right!" "Right!" "Right!" "Wrong!"