Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Parsing Strings to Boolean

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
16 Sep 2010CPOL 22.1K   3   4
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.

C#
// 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.

C#
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)


Written By
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.

Comments and Discussions

 
QuestionSimpler code Pin
Lechuss22-Jun-17 3:35
Lechuss22-Jun-17 3:35 
GeneralReason for my vote of 5 Good one Pin
thatraja29-Oct-10 21:11
professionalthatraja29-Oct-10 21:11 
GeneralThanks, I corrected it. I also changed the arrays that didn'... Pin
Simon Dufour16-Sep-10 9:33
Simon Dufour16-Sep-10 9:33 
GeneralYour code wont compile... You return false without assigning... Pin
jfriedman16-Sep-10 9:11
jfriedman16-Sep-10 9: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.