Click here to Skip to main content
13,297,266 members (71,126 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 16 Sep 2010

Parsing Strings to Boolean

, 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 trueBoolean.Parse("true");// Return falseBoolean.Parse("false");// Exception.. you can prevent that from happening by using a try..catch// or using...
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
// Return false
// Exception.. you can prevent that from happening by using a try..catch
// or using TryParse.
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();
    result = true;
    return true;
  else if (FalseString.Contains(formattedInput))
    result = false;
    return true;
    result = false;
    return false;

*UPDATE* The code now compile. Sorry, I hadn't tested.


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 CGI
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. I worked there until they outsourced all development to CGI. I worked there since.

You may also be interested in...

Comments and Discussions

QuestionSimpler code Pin
Lechuss22-Jun-17 4:35
memberLechuss22-Jun-17 4:35 
GeneralReason for my vote of 5 Good one Pin
thatraja29-Oct-10 22:11
mvpthatraja29-Oct-10 22:11 
GeneralThanks, I corrected it. I also changed the arrays that didn'... Pin
Simon Dufour16-Sep-10 10:33
memberSimon Dufour16-Sep-10 10:33 
GeneralYour code wont compile... You return false without assigning... Pin
jfriedman16-Sep-10 10:11
memberjfriedman16-Sep-10 10:11 

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

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

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