I am outputting the value of a boolean
in my ASP.NET MVC Framework view, and would like to have a lower case true
or false
, rather than the default of True
or False
.
I understand that I could just do this:
@this.Model.MyBool.ToString().ToLower()
Alternatively, I could create an extension method. But this defeats the purpose of:
@this.Model.MyBool
I have read the post Why does Boolean.ToString output "True" and not "true", but most of the answers are pretty old.
Are there any more modern ways to accomplish this?
toString(boolean b) returns a String object representing the specified boolean. If the specified boolean is true, then the string "true" will be returned, otherwise the string "false" will be returned.
To convert Boolean to String in Java, use the toString() method. For this, firstly, we have declared two booleans. String str1 = new Boolean(bool1). toString(); String str2 = new Boolean(bool2).
If you only want this for one bool variable you should use @Mohamed 's method. Else you can create an extension method (as you already said yourself):
public static class Extensions
{
public static string ToLowerString(this bool _bool)
{
return _bool.ToString().ToLower();
}
}
Then to use it:
public static void Main()
{
bool testBoolean = true;
Console.WriteLine(testBoolean.ToLowerString());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With