Casting Y or N to bool C#

前端 未结 10 2197
故里飘歌
故里飘歌 2021-02-07 05:46

Just for neatness sake I was wondering, whether it\'s possible to cast Y or N to a bool? Something like this;

bool theanswer = Convert.ToBoolean(input);
         


        
相关标签:
10条回答
  • 2021-02-07 06:14
    bool theanswer = input.ToLower() == "y";
    
    0 讨论(0)
  • 2021-02-07 06:27

    As suggested by Jon, there's nothing inbuilt like this. The answer posted by John gives you a correct way of doing. Just for more clarification, you can visit:

    http://msdn.microsoft.com/en-us/library/86hw82a3.aspxlink text

    0 讨论(0)
  • 2021-02-07 06:27
    class Program
    {
        void StringInput(string str)
        {
            string[] st1 = str.Split(' ');
    
            if (st1 != null)
            {
                string a = str.Substring(0, 1);
                string b=str.Substring(str.Length-1,1);
    
                 if(
                    a=="^" && b=="^" 
                    || a=="{" && b=="}" 
                    || a=="[" && b=="]"
                    ||a=="<" && b==">" 
                    ||a=="(" && b==")"
                    )
    
                {
                    Console.Write("ok Formate correct");
                }
                else
                {
                    Console.Write("Sorry incorrect formate...");
                }
            }
        }
        static void Main(string[] args)
        {
            ubaid: ;
            Program one = new Program();
            Console.Write("For exit Press N ");
            Console.Write("\n");
            Console.Write("Enter your value...=");
            string ub = Console.ReadLine();
    
            if (ub == "Y" || ub=="y" || ub=="N" || ub=="n" )
            {
                Console.Write("Are your want to Exit Y/N: ");
                string ui = Console.ReadLine();
                if (ui == "Y" || ui=="y")
                {
                    return;
                }
                else
                {
                    goto ubaid;
                }
    
            }
            one.StringInput(ub);           
            Console.ReadLine();
            goto ubaid;
        }
    }
    
    0 讨论(0)
  • 2021-02-07 06:29

    Wonea gave an "IsTrue" source example from DotNetPerls. Here are two shorter versions of it:

    public static bool IsTrue(string value)
    {
        // Avoid exceptions
        if (value == null)
            return false;
    
        // Remove whitespace from string and lowercase it.
        value = value.Trim().ToLower();
    
        return value == "true"
            || value == "t"
            || value == "1"
            || value == "yes"
            || value == "y";
    }
    

    OR:

    private static readonly IReadOnlyCollection<string> LOWER_TRUE_VALUES = new string[] { "true", "t", "1", "yes", "y" };
    
    public static bool IsTrue(string value)
    {
        return value != null
            ? LOWER_TRUE_VALUES.Contains(value.Trim().ToLower())
            : false;
    }
    

    Heck, if you want to get real short (and ugly), you can collapse that down to two lines like this:

    private static readonly IReadOnlyCollection<string> LOWER_TRUE_VALUES = new string[] { "true", "t", "1", "yes", "y" };
    public static bool IsTrue(string value) => value != null ? LOWER_TRUE_VALUES.Contains(value.Trim().ToLower()) : false;
    
    0 讨论(0)
提交回复
热议问题