.net 如何将一个文本评估为多个文本?VB
因为我可以用“if”visual basic比较几个值.net 如何将一个文本评估为多个文本?VB,.net,vba,if-statement,.net,Vba,If Statement,因为我可以用“if”visual basic比较几个值 If TextDisplay.Text = {"/","*","+","-"} Then End If 哪种方法正确?我的Visual Basic不是很好,但我想您需要包含: Dim AllowedCharacters() As String = {"/", "*", "+", "-"} If AllowedCharacters.Contains(TextDisplay.Text) Then 'do something End
If TextDisplay.Text = {"/","*","+","-"} Then
End If
哪种方法正确?我的Visual Basic不是很好,但我想您需要
包含:
Dim AllowedCharacters() As String = {"/", "*", "+", "-"}
If AllowedCharacters.Contains(TextDisplay.Text) Then
'do something
End If
我的Visual Basic不是很好,但我想您需要包含:
Dim AllowedCharacters() As String = {"/", "*", "+", "-"}
If AllowedCharacters.Contains(TextDisplay.Text) Then
'do something
End If
如果只搜索字符而不是整个单词,那么string类已经具有所需的方法
If TextDisplay.Text.IndexOfAny(New Char() {"/"c,"*"c,"+"c,"-"c}) > -1 Then
.....
End If
IndexOfAny返回与所提供列表匹配的第一个字符的索引。(因此需要测试-1,因为零是一个有效的索引)
另请注意附加的字母c
以表示字符常量。如果只搜索字符而不是整个单词,则string类已经具有所需的方法
If TextDisplay.Text.IndexOfAny(New Char() {"/"c,"*"c,"+"c,"-"c}) > -1 Then
.....
End If
IndexOfAny返回与所提供列表匹配的第一个字符的索引。(因此需要测试-1,因为零是一个有效的索引)
另请注意附加的字母c
表示字符常量。看起来像一个计算器,因此假设您只想测试精确的单个字符匹配,您可以简单地:
If ("/*+-".IndexOf(TextDisplay.Text) > 0) Then
或VBA:
If InStr(1, "/*+-", TextDisplay.Text) > 0 Then
(您还需要检查输入字符串是否为空)看起来像一个计算器,因此假设您只想测试精确的单字符匹配,您可以简单地:
If ("/*+-".IndexOf(TextDisplay.Text) > 0) Then
或VBA:
If InStr(1, "/*+-", TextDisplay.Text) > 0 Then
(您还需要检查输入字符串是否为空)这仅在您希望将TestDisplay.Text检查为其中一个值时有效。例如,如果TextDisplay.text==“/Help”,此检查将失败,因为“/Help”不在AllowedCharacters集中。如果您试图检查TextDisplay.Text中是否有任何“AllowedCharacters”,则可能需要与Steve回答的内容更接近的内容。这实际上只适用于您希望检查TestDisplay.Text是否仅为这些值之一的情况。例如,如果TextDisplay.text==“/Help”,此检查将失败,因为“/Help”不在AllowedCharacters集中。如果您试图检查TextDisplay.Text中是否有任何“AllowedCharacters”,那么您可能需要与Steve回答的内容更接近的内容。