如何用Case检测参数

 Caroline19921009 发布于 2023-02-06 14:43

我正在尝试创建一系列可以获取参数的命令.拿起我正在使用Select Case的每个单独的命令这个问题是如果我使用的话,我无法感知'参数'(字符串的第二部分)Case Else.如果我不使用Case Else那么我无法处理不正确的命令并将它们发送到所需的程序.

例如:

Private Sub AllocateType(ByVal Command As String)

    Select Case Command
        Case "Eat"
            'Call Eat procedure
        Case "Use"
            'Call Use procedure
        Case "Quit"

        Case "Pause"

        Case "Go"

        Case Else
            Errors() 'Error handling procedure
    End Select

End Sub

如果命令是'Brrr​​rr',它会调用Errors().然而,如果命令是"吃食物",它仍会调用Errors()而不是将参数传递给Eat程序.

编辑,因为它现在不起作用. 我已经尝试了所建议的内容,但我仍然有完全相同的问题.它似乎兼而有之Command.StartsWith,Command.Contains因为如果我尝试进入'吃食物',它仍然不会认为它是一个案例.

例:

Select Case Command
    Case Command.Contains("Eat")
        Output("TESTING")
        If Len(Command) > 4 Then
            Command = Mid(Command, 4, (Len(Command) - 4))
            Interaction(Command)
        Else
            Output("Eat What?")
        End If
    Case "Eat"
        Output("Eat What?")
    Case Command.StartsWith("Use")
        If Len(Command) > 4 Then
            Command = Mid(Command, 4, (Len(Command) - 4))
            Interaction(Command)
        Else
            Output("Use What?")
        End If
    Case "Use"
        Output("Use What?")
        'Case Else
        '    Errors()
End Select

Ashley Medwa.. 5

您需要使用IF语句来执行所需的检查.
你有两个选择StartsWithContains.
StartsWith将允许您检查您的关键字是否在字符串的开头.

吃香蕉=真

香蕉吃=假

Contains 将允许您检查您的关键字是否退出任何地方.

吃香蕉=真

香蕉吃=真

StartsWith Example

Private Sub AllocateType(ByVal Command As String)

    If Command.StartsWith("Eat") Then
        'Call Eat procedure
    Else If Command.StartsWith("Use") Then
        'Call Use procedure
    Else If Command.StartsWith("Quit") Then
        'Call Quit procedure
    Else If Command.StartsWith("Pause") Then
        'Call Pause procedure
    Else If Command.StartsWith("Go") Then
        'Call Go procedure
    Else
        Errors()
    End If

End Sub

包含示例

Private Sub AllocateType(ByVal Command As String)

    If Command.Contains("Eat") Then
        'Call Eat procedure
    Else If Command.Contains("Use") Then
        'Call Use procedure
    Else If Command.Contains("Quit") Then
        'Call Quit procedure
    Else If Command.Contains("Pause") Then
        'Call Pause procedure
    Else If Command.Contains("Go") Then
        'Call Go procedure
    Else
        Errors()
    End If

End Sub

另外,StartsWith或者Contains你可以这样做,StringComparison这将允许你忽略大小写.

If Command.StartsWith("Eat", StringComparison.OrdinalIgnoreCase) Then

要么

If Command.Contains("Eat", StringComparison.OrdinalIgnoreCase) Then

忽略该案例将匹配"EAT","Eat","eAt"等.

1 个回答
  • 您需要使用IF语句来执行所需的检查.
    你有两个选择StartsWithContains.
    StartsWith将允许您检查您的关键字是否在字符串的开头.

    吃香蕉=真

    香蕉吃=假

    Contains 将允许您检查您的关键字是否退出任何地方.

    吃香蕉=真

    香蕉吃=真

    StartsWith Example

    Private Sub AllocateType(ByVal Command As String)
    
        If Command.StartsWith("Eat") Then
            'Call Eat procedure
        Else If Command.StartsWith("Use") Then
            'Call Use procedure
        Else If Command.StartsWith("Quit") Then
            'Call Quit procedure
        Else If Command.StartsWith("Pause") Then
            'Call Pause procedure
        Else If Command.StartsWith("Go") Then
            'Call Go procedure
        Else
            Errors()
        End If
    
    End Sub
    

    包含示例

    Private Sub AllocateType(ByVal Command As String)
    
        If Command.Contains("Eat") Then
            'Call Eat procedure
        Else If Command.Contains("Use") Then
            'Call Use procedure
        Else If Command.Contains("Quit") Then
            'Call Quit procedure
        Else If Command.Contains("Pause") Then
            'Call Pause procedure
        Else If Command.Contains("Go") Then
            'Call Go procedure
        Else
            Errors()
        End If
    
    End Sub
    

    另外,StartsWith或者Contains你可以这样做,StringComparison这将允许你忽略大小写.

    If Command.StartsWith("Eat", StringComparison.OrdinalIgnoreCase) Then
    

    要么

    If Command.Contains("Eat", StringComparison.OrdinalIgnoreCase) Then
    

    忽略该案例将匹配"EAT","Eat","eAt"等.

    2023-02-06 14:46 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有