简体   繁体   中英

C# do while Enter uppercase and lowercase letters in a string

C # do while question.

What I want is the ability to terminate Q or q in upper case, lowercase letters in the source code.

I referenced MSDN but it did not work out, so I have a question here.

Thank you.

using System;

namespace Try
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "Q";
            string str2 = "q";

            if (str1.Equals(str2, StringComparison.OrdinalIgnoreCase)) { }

            string menu = "";

            do
            {
                Console.WriteLine("Select Meun:(1)Triangle (2)Rectangle " +
                                  "(Q)Quit",string.Equals
                                  (str1, str2, StringComparison.CurrentCultureIgnoreCase));

                menu = Console.ReadLine();

                Console.WriteLine(menu + "is selected");
            } while (menu != "Q");
        }
     }
}

Option 1

while (!string.Equals(menu, "Q", StringComparison.OrdinalIgnoreCase));

As the StringComparision suggests, this will ignore the case and treat q and Q as the same.

static void Main(string[] args)
{
    string str1 = "Q";
    string str2 = "q";

    if (str1.Equals(str2, StringComparison.OrdinalIgnoreCase)) { }

    string menu = "";

    do
    {
        Console.WriteLine("Select Meun:(1)Triangle (2)Rectangle " +
                "(Q)Quit",string.Equals
                (str1, str2, 
                StringComparison.CurrentCultureIgnoreCase));

        menu = Console.ReadLine();

        Console.WriteLine(menu + "is selected");
    } while (!string.Equals(menu, "Q", StringComparison.OrdinalIgnoreCase));
}

Option 2

while (menu.ToUpper() != "Q") 

This will convert anything that is in the variable menu to uppercase. It's easier to read and does the job and I personally prefer this method.

static void Main(string[] args)
{
    string str1 = "Q";
    string str2 = "q";

    if (str1.Equals(str2, StringComparison.OrdinalIgnoreCase)) { }

    string menu = "";

    do
    {
        Console.WriteLine("Select Meun:(1)Triangle (2)Rectangle " +
                "(Q)Quit",string.Equals
                (str1, str2, 
                StringComparison.CurrentCultureIgnoreCase));

        menu = Console.ReadLine();

        Console.WriteLine(menu + "is selected");
    } while (menu.ToUpper() != "Q");
}

Addtional There's also the example below that uses switch statements which are great and easier to read if you plan on having a lot of options in the future:

do
{
    Console.WriteLine("Select Menu:(1)Triangle (2)Rectangle (Q)Quit");

     menu = Console.ReadLine();
     switch (menu.ToUpper())
     {
         case "1":
            //DO SOME CODE
            break;
         case "2":
            //DO SOME CODE
            break;
         case "Q":
            return;
         }

         Console.WriteLine(menu + " is selected");
 } while (true);

Or this method which doesn't use any StringComparison or ToUpper and would be use if you would want separate upper and lower case commands, or if you dont want to do any additional conversion/checking.

do
{
      Console.WriteLine("Select Menu:(1)Triangle (2)Rectangle (Q)Quit");

      menu = Console.ReadLine();
      switch (menu)
      {
          case "1":
             //DO THIS
             break;
          case "2":
             //DO THAT
             break;
          case "q":
          case "Q":
             return;
      }

      Console.WriteLine(menu + " is selected");
} while (true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM