Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Parsing Strings to Boolean

By , 16 Sep 2010
Rate this:
Please Sign up or sign in to vote.
If you don't need to check for other strings than True or False, then, you should use Boolean.Parse or Boolean.TryParse.
 
// Return true
Boolean.Parse("true");
// Return false
Boolean.Parse("false");
// Exception.. you can prevent that from happening by using a try..catch
// or using TryParse.
Boolean.Parse("foo");
 
bool isBool;
if(Boolean.TryParse("true", out isBool))
{
  // If we get in here, we know that isBool is a valid boolean, 
  // either "True" or "False".
}
 
bool isBool2;
if(Boolean.TryParse("foo", out isBool2))
{
  //We won't get in here because "foo" isn't a boolean.
}
 
Otherwise, I'd use this modified version of TryParse. If the result isn't True, or False, the method will return False. If the result is found, the method will return True. The result can be found in the out parameter.
 
private static readonly List<string> TrueString = new List<string>(new string[]{"true", "t", "1", "yes", "y"});
 
private static readonly List<string> FalseString = new List<string>(new string[]{"false", "f", "0", "no", "n"});
 
public static bool TryConvertToBoolean(string input, out bool result)
{
  // Remove whitespace from string and lowercase
  string formattedInput = input.Trim().ToLower();
  
  if(TrueString.Contains(formattedInput))
  {
    result = true;
    return true;
  }
  else if (FalseString.Contains(formattedInput))
  {
    result = false;
    return true;
  }
  else
  {
    result = false;
    return false;
  }
}
 
*UPDATE* The code now compile. Sorry, I hadn't tested.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Simon Dufour
Software Developer SNC-Lavalin inc.
Canada Canada
I have a three years technical education in computer science (June 2006) and a four years bachelor's degree in Software Engineering from the École de Technologie Supérieure (August 2010). I worked as a Web Developper at the beginning of my career before being recruited as a .NET Analyst-Programer at SNC-Lavalin in Montreal.
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralReason for my vote of 5 Good one Pinmvpthatraja29-Oct-10 21:11 
GeneralThanks, I corrected it. I also changed the arrays that didn'... PinmemberSimon Dufour16-Sep-10 9:33 
GeneralYour code wont compile... You return false without assigning... Pinmemberjfriedman16-Sep-10 9:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 16 Sep 2010
Article Copyright 2010 by Simon Dufour
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid