Click here to Skip to main content
11,932,168 members (54,522 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked

Parsing Strings to Boolean

, 15 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Handy extension method that parses common boolean expressions
public static class StringExtensions
        private static readonly string[] ValidString = 
                       new[] {"true", "t", "1", "yes", "y"};
        public static bool TryConvertToBoolean(this string input)
                if (input == null) return false;
                // Remove whitespace from string and lowercase
                input = input.Trim().ToLower();
                return ValidString.Contains(input);
                return false;


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


About the Author

Enrique Albert
Software Developer (Senior)
Ireland Ireland
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 3 Pin
ProgramFOX1-Feb-13 7:47
member ProgramFOX1-Feb-13 7:47 
GeneralRe: My vote of 3 Pin
Enrique Albert2-Feb-13 6:48
memberEnrique Albert2-Feb-13 6:48 
GeneralI don't really like your method. It implies that if the stri... Pin
Simon Dufour16-Sep-10 5:24
memberSimon Dufour16-Sep-10 5:24 
GeneralRe: I don't really like your method. It implies that if the stri... Pin
Enrique Albert2-Feb-13 6:50
memberEnrique Albert2-Feb-13 6:50 
GeneralOn the way to a coding horror Pin
bhiller20-Sep-10 21:56
memberbhiller20-Sep-10 21:56 
Congratulation! Somewhen in the future, somebody maintaining that application will experience a horror similiar to[^]. Smile | :)
GeneralRe: On the way to a coding horror Pin
Enrique Albert20-Sep-10 22:43
memberEnrique Albert20-Sep-10 22:43 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 15 Sep 2010
Article Copyright 2010 by Enrique Albert
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid