Parse string to bool value
In this chapter you will learn:
Convert string to bool
using System;/*j a v a 2s. c o m*/
public class Example
{
public static void Main()
{
try {
bool flag = Boolean.Parse("True");
flag = Boolean.Parse("False");
flag = Boolean.Parse("true");
flag = Boolean.Parse("false");
flag = Boolean.Parse(" true ");
flag = Boolean.Parse("0");
flag = Boolean.Parse("1");
flag = Boolean.Parse("-1");
flag = Boolean.Parse("asdf");
}
catch (ArgumentException) {
Console.WriteLine("Cannot parse a null string.");
}
catch (FormatException) {
Console.WriteLine("Cannot parse '{0}'.", value);
}
}
}
Next chapter...
What you will learn in the next chapter: