2012-03-16 8 views
38

मेरे पास string है जो या तो "0" या "1" हो सकता है, और यह गारंटी है कि यह और कुछ नहीं होगा।स्ट्रिंग को एक बूल में परिवर्तित करने के लिए कैसे करें

तो सवाल यह है कि इसे bool में बदलने के लिए सबसे अच्छा, सरल और सबसे सुरुचिपूर्ण तरीका क्या है?

धन्यवाद।

+0

किसी भी अप्रत्याशित मान इनपुट में हो सकता है, तो TryParse (http://stackoverflow.com/questions/18329001/parse-to-boolean-or-check-string-value/ उपयोग करने के लिए पर विचार 18329085 # 18329085) –

उत्तर

113

काफी वास्तव में सरल:

bool b = str == "1"; 
15
bool b = str.Equals("1")? true : false; 

या और भी बेहतर, नीचे एक टिप्पणी में सुझाव के रूप में:

bool b = str.Equals("1"); 
+28

मैं प्रपत्र 'x के कुछ भी मानता हूं? सच: झूठा 'विनोदी। –

+4

'बूल बी = str.Equals ("1")' पहली नज़र में ठीक और अधिक सहज ज्ञान युक्त काम करता है। –

37

इस सवाल की विशिष्ट जरूरतों की अनदेखी कर, और जबकि इसकी कभी नहीं एक अच्छा एक बूल को स्ट्रिंग डालने का विचार, एक तरीका कनवर्ट क्लास पर ToBoolean() विधि का उपयोग करना होगा:

public static bool ToBoolean(this string s) 
    { 
     string[] trueStrings = { "1", "y" , "yes" , "true" }; 
     string[] falseStrings = { "0", "n", "no", "false" }; 


     if (trueStrings.Contains(s, StringComparer.OrdinalIgnoreCase)) 
      return true; 
     if (falseStrings.Contains(s, StringComparer.OrdinalIgnoreCase)) 
      return false; 

     throw new InvalidCastException("only the following are supported for converting strings to boolean: " 
      + string.Join(",", trueStrings) 
      + " and " 
      + string.Join(",", falseStrings)); 
    } 
+0

कनवर्ट का व्यवहार। टोबोलियन http://stackoverflow.com/questions/7031964/what-is-the-difference-between-convert-tobooleanstring-and-boolean-parsestrin/26202581#26202581 –

5

मैं मोहम्मद सेफावांड की अवधारणा पर पिगीबैकिंग कुछ थोड़ा और अधिक विस्तृत बनाया,:bool boolVal = Convert.ToBoolean("true");

या एक विस्तार विधि जो कुछ भी अजीब मानचित्रण आप कर रहे हैं करने के लिए पता है कि यह आपके प्रश्न का उत्तर नहीं देता है, बल्कि अन्य लोगों की मदद के लिए। "सही" या "गलत" तार बूलियन के लिए आप कन्वर्ट करने के लिए कोशिश कर रहे हैं:

Boolean.Parse प्रयास करें

bool val = Boolean.Parse("true"); ==> true 
bool val = Boolean.Parse("True"); ==> true 
bool val = Boolean.Parse("TRUE"); ==> true 
bool val = Boolean.Parse("False"); ==> false 
bool val = Boolean.Parse("1"); ==> Exception! 
bool val = Boolean.Parse("diffstring"); ==> Exception! 
13

मैं:

public static class MyStringExtensions 
{ 
    public static bool ToBoolean(this string value) 
    { 
     switch (value.ToLower()) 
     { 
      case "true": 
       return true; 
      case "t": 
       return true; 
      case "1": 
       return true; 
      case "0": 
       return false; 
      case "false": 
       return false; 
      case "f": 
       return false; 
      default: 
       throw new InvalidCastException("You can't cast a weird value to a bool!"); 
     } 
    } 
} 
+0

में दिखाया गया यह एक पावरहेल स्क्रिप्ट के लिए आवश्यक है कुछ एक्सएमएल डेटा पढ़ना और यह सही है! – Alternatex

2

यहां सबसे क्षमा स्ट्रिंग पर मेरे प्रयास रूपांतरण अभी भी उपयोगी है कि bool करने के लिए, मूल रूप से कुंजीयन है केवल पहले चरित्र से बाहर।

public static class StringHelpers 
{ 
    /// <summary> 
    /// Convert string to boolean, in a forgiving way. 
    /// </summary> 
    /// <param name="stringVal">String that should either be "True", "False", "Yes", "No", "T", "F", "Y", "N", "1", "0"</param> 
    /// <returns>If the trimmed string is any of the legal values that can be construed as "true", it returns true; False otherwise;</returns> 
    public static bool ToBoolFuzzy(this string stringVal) 
    { 
     string normalizedString = (stringVal?.Trim() ?? "false").ToLowerInvariant(); 
     bool result = (normalizedString.StartsWith("y") 
      || normalizedString.StartsWith("t") 
      || normalizedString.StartsWith("1")); 
     return result; 
    } 
} 
1

मैंने स्ट्रिंग को बूलियन में बदलने के लिए नीचे दिए गए कोड का उपयोग किया।

Convert.ToBoolean(Convert.ToInt32(myString)); 
+0

Convert.ToInt32 को कॉल करना अनावश्यक है यदि केवल दो संभावनाएं "1" और "0" हैं। यदि आप अन्य मामलों पर विचार करना चाहते हैं, तो var isTue = Convert.ToBoolean ("true") == true && Convert.ToBoolean ("1"); // दोनों सच हैं। – TamusJRoyce

+0

मोहम्मद Sepahvand जवाब माइकल Freidgeim टिप्पणी पर देखो! – TamusJRoyce

0
private static readonly ICollection<string> PositiveList = new Collection<string> { "Y", "Yes", "T", "True", "1", "OK" }; 

public static bool ToBoolean(this string input) 
{ 
       return input != null && PositiveList.Any(λ => λ.Equals(input, StringComparison.OrdinalIgnoreCase)); 
} 
संबंधित मुद्दे

 संबंधित मुद्दे