回答
很實際很簡單:
bool b = str == "1";
bool b = str.Equals("1")? true : false;
甚至更好,如建議在下面留言:
bool b = str.Equals("1");
我認爲形式爲'x?真實:假幽默。 – 2012-03-16 18:48:23
'bool b = str.Equals(「1」)'乍一看很好,更直觀。 – 2012-03-16 18:52:53
忽略這個問題的具體需求,雖然其不是一個好想法將一個字符串投射到布爾,一種方法是在Convert類上使用ToBoolean()方法:
bool boolVal = Convert.ToBoolean("true");
或擴展方法做你正在做什麼奇怪的映射:
public static class MyStringExtensions
{
public static bool ToBoolean(this string value)
{
switch (value.ToLower())
{
case "true":
return true;
case "t":
return true;
case "1":
return true;
case "0":
return false;
case "false":
return false;
case "f":
return false;
default:
throw new InvalidCastException("You can't cast a weird value to a bool!");
}
}
}
Convert.ToBoolean的行爲顯示在http://stackoverflow.com/questions/7031964/what-is-the-difference-between-convert-tobooleanstring-and-boolean-parsestrin/26202581#26202581 – 2017-01-30 11:35:37
我做的東西一點點擴展,捎帶上穆罕默德Sepahvand的概念:
public static bool ToBoolean(this string s)
{
string[] trueStrings = { "1", "y" , "yes" , "true" };
string[] falseStrings = { "0", "n", "no", "false" };
if (trueStrings.Contains(s, StringComparer.OrdinalIgnoreCase))
return true;
if (falseStrings.Contains(s, StringComparer.OrdinalIgnoreCase))
return false;
throw new InvalidCastException("only the following are supported for converting strings to boolean: "
+ string.Join(",", trueStrings)
+ " and "
+ string.Join(",", falseStrings));
}
我知道這不會回答你的問題,而只是爲了幫助其他人。如果你正試圖轉換「真」或「假」的字符串爲布爾值:
嘗試Boolean.Parse
bool val = Boolean.Parse("true"); ==> true
bool val = Boolean.Parse("True"); ==> true
bool val = Boolean.Parse("TRUE"); ==> true
bool val = Boolean.Parse("False"); ==> false
bool val = Boolean.Parse("1"); ==> Exception!
bool val = Boolean.Parse("diffstring"); ==> Exception!
需要Powershell腳本閱讀一些XML數據,這是完美的! – Alternatex 2017-10-12 10:37:13
這是我嘗試以最寬容的字符串爲bool的轉換是還是有用的,基本上鍵控只關閉第一個字符。
public static class StringHelpers
{
/// <summary>
/// Convert string to boolean, in a forgiving way.
/// </summary>
/// <param name="stringVal">String that should either be "True", "False", "Yes", "No", "T", "F", "Y", "N", "1", "0"</param>
/// <returns>If the trimmed string is any of the legal values that can be construed as "true", it returns true; False otherwise;</returns>
public static bool ToBoolFuzzy(this string stringVal)
{
string normalizedString = (stringVal?.Trim() ?? "false").ToLowerInvariant();
bool result = (normalizedString.StartsWith("y")
|| normalizedString.StartsWith("t")
|| normalizedString.StartsWith("1"));
return result;
}
}
我用下面的代碼將字符串轉換爲布爾值。
Convert.ToBoolean(Convert.ToInt32(myString));
如果只有兩種可能性是「1」和「0」,則不必調用Convert.ToInt32。如果你想考慮其他情況,var isTrue = Convert.ToBoolean(「true」)== true && Convert.ToBoolean(「1」); //都是真的。 – TamusJRoyce 2017-02-08 15:06:42
看穆罕默德Sepahvand回答Michael Freidgeim評論! – TamusJRoyce 2017-02-08 15:49:32
private static readonly ICollection<string> PositiveList = new Collection<string> { "Y", "Yes", "T", "True", "1", "OK" };
public static bool ToBoolean(this string input)
{
return input != null && PositiveList.Any(λ => λ.Equals(input, StringComparison.OrdinalIgnoreCase));
}
- 1. 將字符串轉換爲布爾
- 2. 將字符串轉換爲布爾值
- 3. 將字符串轉換爲布爾值並檢查布爾值
- 4. 不能將字符串轉換爲布爾,但沒有布爾?
- 5. 將字符串轉換成布爾
- 6. Ruby:如何將字符串轉換爲布爾型
- 7. 如何將字符串轉換爲布爾值?
- 8. 如何將搜索字符串轉換爲布爾條件?
- 9. 如何將字符串轉換爲布爾表達式
- 10. 如何將字符串對象轉換爲布爾對象?
- 11. 如何將布爾值轉換爲本地化字符串
- 12. 如何將字符串轉換爲布爾數組?
- 13. 如何在ActionScript中將字符串轉換爲布爾值?
- 14. 如何將這些字符串轉換爲布爾值?
- 15. 如何將字符串轉換爲布爾值?
- 16. 如何將字符串轉換爲JavaScript中的布爾值?
- 17. 如何將「False」字符串轉換爲布爾值false?
- 18. 如何將一個字符串轉換爲布爾值?
- 19. XSLT:如何將字符串值轉換爲布爾變量?
- 20. 如何將布爾表達式字符串轉換爲對象?
- 21. 如何使用推土機將布爾轉換爲字符串?
- 22. 我們如何將布爾轉換爲Haskell中的字符串
- 23. 字符串到布爾轉換行爲
- 24. 在C#中將布爾值轉換爲字符串值
- 25. 將布爾轉換爲字符串在Datagridview中?
- 26. BASH將字符串轉換爲布爾變量
- 27. 無法將類型字符串轉換爲布爾型
- 28. Scala:將字符串轉換爲布爾值的優雅
- 29. C將字符串轉換爲布爾數組
- 30. 在Julia中將字符串轉換爲布爾值
如果有任何意外的值可以在輸入,考慮到使用的TryParse(http://stackoverflow.com/questions/18329001/parse-to-boolean-or-check-string-value/ 18329085#18329085) – 2017-01-30 11:33:00