如何比较不区分大小写的字符串?

时间:2018-07-28 21:36:52

标签: c# string switch-statement do-while case-sensitive

我正在编写一个代码,如果用户需要功能,则必须按“ y”或“ n”或输入“ yes”或“ no”,而无需检查是否区分大小写(因此,如果用户写yES或YeS,则需要工作。

此外,如果用户写了任何无效的内容,则表示此选项无效,并再次要求用户选择一个选项。

这里是它的抽象:

    static bool FeatureIsOn { get; set;}

    static void Main()
    {
        bool optionIsValid;

        do //Loops around until the option is valid
        {
            Console.WriteLine();
            Console.Write("Enable Feature? [Y/N]: ");
            string optionString =  Console.ReadLine();

            switch(optionString)
            {
                default:
                    Console.WriteLine("Invalid option.");
                    optionIsValid = false;
                break;

                case "Yes":
                case "yes":
                case "Y":
                case "y":
                    FeatureIsOn = true;
                    optionIsValid = true;
                break;

                case "No":
                case "no":
                case "N":
                case "n":
                    FeatureIsOn = false;
                    optionIsValid = true;
                break;

            }
        } while (optionIsValid != true);
    }

为每种可能的方式写一个“是”不是很有效。有更好的方法吗?

3 个答案:

答案 0 :(得分:4)

在检查之前将要检查的字符串转换为大写或小写:

static bool FeatureIsOn { get; set;}

static void Main()
{
    bool optionIsValid;

    do //Loops around until the option is valid
    {
        Console.WriteLine();
        Console.Write("Enable Feature? [Y/N]: ");
        string optionString =  Console.ReadLine();

        // convert string to uppercase 
        optionString = optionString.ToUpper();

        switch(optionString)
        {
            case "YES":
            case "Y":
                FeatureIsOn = true;
                optionIsValid = true;
            break;

            case "NO":
            case "N":
                FeatureIsOn = false;
                optionIsValid = true;
            break;

            default:
                Console.WriteLine("Invalid option.");
                optionIsValid = false;
            break;

        }
    } while (optionIsValid != true);
}

答案 1 :(得分:1)

您可以使用类似ERROR: C:\Users\-user-\Desktop\dw_parser.py:9: Symbol 'EQUALS' used, but not defined as a token or a rule WARNING: Token 'COLON' defined, but not used WARNING: Token 'DIVIDE' defined, but not used WARNING: Token 'EQUAL' defined, but not used WARNING: Token 'EQUAL_TO' defined, but not used WARNING: Token 'GREATER' defined, but not used WARNING: Token 'GREATER_EQL' defined, but not used WARNING: Token 'LESS' defined, but not used WARNING: Token 'LESS_EQL' defined, but not used WARNING: Token 'LPAREN' defined, but not used WARNING: Token 'MINUS' defined, but not used WARNING: Token 'QUOTATION' defined, but not used WARNING: Token 'REMAINDER' defined, but not used WARNING: Token 'RPAREN' defined, but not used WARNING: Token 'SEMICOLON' defined, but not used WARNING: Token 'STRING' defined, but not used WARNING: There are 15 unused tokens ERROR: Infinite recursion detected for symbol 'assign' ERROR: Infinite recursion detected for symbol 'expr' Traceback (most recent call last): File "C:\Users\-user-\Desktop\dw_parser.py", line 35, in <module> parser = yacc.yacc() File "C:\Program Files\Python37\lib\site-packages\ply-3.11-py3.7.egg\ply\yacc.py", line 3432, in yacc raise YaccError('Unable to build parser') ply.yacc.YaccError: Unable to build parser 的方法,然后将输入的字符串与YES,NO,Y,N进行比较(也可以与String.ToUpper()一起使用,但是我不确定这样做会更快。

否则,也许某些String.ToLower()会给出更清晰的结果,但不会改变速度。

EDIT:另一个选择是使用if/else而不是Console.Read(),因此,如果用户想要输入'YES',它将仅保留'Y'(仅记录一个字符),这将允许您将测试除以2;)

答案 2 :(得分:0)

先修剪一下,再修剪一下,然后验证它是否包含有效的选项(n,y或否,是),然后找出实际选择是什么,只需使用.contains来检查y。这是我的策略。