public static class StringExtensions { private static readonly string[] ValidString = new[] {"true", "t", "1", "yes", "y"}; public static bool TryConvertToBoolean(this string input) { try { if (input == null) return false; // Remove whitespace from string and lowercase input = input.Trim().ToLower(); return ValidString.Contains(input); } catch { return false; } }
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
string
true
false
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.