C# में किसी स्ट्रिंग को बूलियन (bool / Boolean) में बदलने के लिए bool.Parse() या bool.TryParse() मेथड्स का उपयोग किया जाता है।
उदाहरण: Parse() का उपयोग
using System;
class Demo
{
static void Main()
{
string str = "false";
// Parse() - "True" या "False" (केस-इनसेंसिटिव) स्वीकार करता है
bool result = bool.Parse(str);
Console.WriteLine($"स्ट्रिंग: \"{str}\"");
Console.WriteLine($"Bool मान: {result}");
}
}
आउटपुट
स्ट्रिंग: "false" Bool मान: False
स्वीकार्य स्ट्रिंग मान
bool.Parse() और bool.TryParse() केवल निम्नलिखित स्ट्रिंग्स (केस-इनसेंसिटिव) को स्वीकार करते हैं:
| स्ट्रिंग मान | परिणाम |
|---|---|
| "True", "true", "TRUE", "TrUe" | true |
| "False", "false", "FALSE", "FaLsE" | false |
नोट: "1", "0", "Yes", "No", "Y", "N" आदि स्वीकार नहीं किए जाते और FormatException फेंकेंगे।
सुरक्षित रूपांतरण: TryParse() का उपयोग
using System;
public class Demo
{
public static void Main()
{
string[] testStrings =
{
"true", "True", "TRUE", // वैध true
"false", "False", "FALSE", // वैध false
"1", "0", // अवैध - अपवाद/झूठा
"yes", "no", // अवैध
"", " ", "abc" // अवैध
};
foreach (string s in testStrings)
{
Console.Write($"इनपुट: \"{s}\" -> ");
if (bool.TryParse(s, out bool result))
{
Console.WriteLine($"सफल: {result}");
}
else
{
Console.WriteLine("विफल - केवल 'true' या 'false' स्वीकार्य हैं");
}
}
}
}
आउटपुट
इनपुट: "true" -> सफल: True इनपुट: "True" -> सफल: True इनपुट: "TRUE" -> सफल: True इनपुट: "false" -> सफल: False इनपुट: "False" -> सफल: False इनपुट: "FALSE" -> सफल: False इनपुट: "1" -> विफल - केवल 'true' या 'false' स्वीकार्य हैं इनपुट: "0" -> विफल - केवल 'true' या 'false' स्वीकार्य हैं इनपुट: "yes" -> विफल - केवल 'true' या 'false' स्वीकार्य हैं इनपुट: "no" -> विफल - केवल 'true' या 'false' स्वीकार्य हैं इनपुट: "" -> विफल - केवल 'true' या 'false' स्वीकार्य हैं इनपुट: " " -> विफल - केवल 'true' या 'false' स्वीकार्य हैं इनपुट: "abc" -> विफल - केवल 'true' या 'false' स्वीकार्य हैं
अन्य मानों को Bool में बदलने के विकल्प
using System;
public class Demo
{
public static void Main()
{
// 1. Convert.ToBoolean() - अधिक लचीला
Console.WriteLine("Convert.ToBoolean():");
Console.WriteLine(Convert.ToBoolean("true")); // True
Console.WriteLine(Convert.ToBoolean("false")); // False
Console.WriteLine(Convert.ToBoolean("1")); // True (स्ट्रिंग "1" के लिए भी काम करता है!)
Console.WriteLine(Convert.ToBoolean("0")); // False
Console.WriteLine(Convert.ToBoolean(null)); // False
// 2. कस्टम पार्सिंग लॉजिक
Console.WriteLine("\nकस्टम पार्सिंग:");
string[] inputs = { "yes", "no", "y", "n", "1", "0", "on", "off" };
foreach (string input in inputs)
{
bool result = ParseFlexibleBool(input);
Console.WriteLine($"\"{input}\" -> {result}");
}
}
static bool ParseFlexibleBool(string input)
{
if (string.IsNullOrWhiteSpace(input)) return false;
string lower = input.Trim().ToLowerInvariant();
return lower switch
{
"true" or "1" or "yes" or "y" or "on" => true,
"false" or "0" or "no" or "n" or "off" => false,
_ => false // या अपवाद फेंकें
};
}
}
आउटपुट
Convert.ToBoolean(): True False True False False कस्टम पार्सिंग: "yes" -> True "no" -> False "y" -> True "n" -> False "1" -> True "0" -> False "on" -> True "off" -> False
महत्वपूर्ण बिंदु
bool.Parse()केवल "true"/"false" (केस-इनसेंसिटिव) स्वीकार करता हैConvert.ToBoolean()"1"/"0" और null को भी संभालता है- उपयोगकर्ता इनपुट के लिए कस्टम पार्सिंग लॉजिक लिखें
TryParse()अपवाद से बचने के लिए अनुशंसित है- कॉन्फिग फाइल्स, कमांड-लाइन आर्ग्यूमेंट्स, यूजर इनपुट के लिए लचीला पार्सिंग उपयोगी है